-2

enter image description here

When I run the code I get ClassNotFoundException.I thought it was because of ojdbc6.jar file.I downloaded the file from Oracle and copied the file to my subdirectory from where I am running the code on command line.However, I am still getting the error.Can anyone help me understanding what am I doing wrong? enter image description here

Kalia Dona
  • 381
  • 1
  • 3
  • 11

4 Answers4

4

You're having this problem because the class file that you try to run depends on a jar file called ojdbc6.jar. First , you need to compile both, the jar file and the class file.

1) First put your jar file in the same directory where you have your java code.

2) Then compile both, jar and java file:

javac -cp ".:/path_of_jar_file_found_using_pwd/ojdbc6.jar" MyJavaFile.java

3) Now run both as:

java -cp ojbdc6.jar:. MyJavaFile

This should work on mac/linux or other unix based system.For windows, replace : by ; .

1

From where are you running your program? From the screenshot, I can see that you are running it from terminal

Use the below command to run it from the terminal supplying the necessary jars which includes them in classpath for this particular run.

java -cp "Test.jar;lib/*" my.package.MainClass

If you want the necessary jars, you can set the classpath variable in windows by the below command and add necessary jars to the existing classpath variable

SET CLASSPATH = %CLASSPATH%;Test.jar;C:/username/Test1.jar

And then, you can run the java program normally

java SimpleInsert

If you are using an IDE like eclipse, NetBeams, you have to set up your build path and external jars in your build path.

Refer this post for more information Setting multiple jars in java classpath

Community
  • 1
  • 1
ShankarDaruga
  • 136
  • 1
  • 5
  • I'm running it on mac which is unix like. So how do I set classpath for that? I think mac command for this should be similar to Linux. Although I already accepted some else's answer, if you can answer this I will upvote your answer.And thank you in advance. – Kalia Dona Feb 22 '17 at 07:11
  • And also what is java -cp "lib/*" ? By Test,jar, do you mean my ojdbc6.jar file? And my.package.MainClass , do you mean .java file? – Kalia Dona Feb 22 '17 at 07:14
  • I'm not sure about Mac, but in unix, you can use this `export CLASSPATH = $CLASSPATH:/home/user/ojdbc6.jar:/lib/*` before running your program. And.. java -cp "lib/*" will add all the jar files present in /lib folder. This is useful if you are using more jars. You can place them in the lib folder (or any folder of your choice ) and add all the jar present in that folder with the above wildcard use "lib/*" – ShankarDaruga Feb 22 '17 at 09:07
0

As mentioned in my comment earlier, you might consider adding ojdbc6.jar to your CLASSPATH variable or you might add this jar under your %JAVA_HOME%\jre\lib\ext folder so that the extension classloader will be able to load the necessary class file.

SET CLASSPATH = %CLASSPATH%;C:/JARS/ojdbc6.jar;

You can easily check whether the necessary class is part of your Application using CTRL + SHIFT + T, as it identifies classes from added JARs as well.

Hope this helps!

N00b Pr0grammer
  • 3,930
  • 4
  • 28
  • 40
0
  1. Right-click on warning message --> Quick Fix;

  2. Select the option Mark the associated raw classpath entry as a publish/export dependency. (in the following figure)