0

I have a Java Main class that use an external .jar library.

So I am trying to compile it putting this .jar file into the classpath doing:

C:\Projects\edi-sta\src>javac -cp ojdbc6.jar:. Main.java

So I think that it should mens that I have to compile the Main.Java class "importing" the classpath represented by the ojdbc6.jar.

It give me no error message and compile it but the problem is that when I try to perform the compiled Main class I obtain this error message:

C:\Projects\edi-sta\src>java Main
Hello World !!!
0
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at Main.main(Main.java:21)

C:\Projects\edi-sta\src>

It can't see the oracle.jdbc.OracleDriver class definied into the ojdbc6.jar used as classpath at compile time.

Why I have this issue? What am I missing? How can I fix it?

EDIT 1:

I mooved my Main.java class into a package named mainPkg.

So I have the following situation in which I have also putted the ojdbc6.jar file:

C:\Projects\edi-sta\src\mainPkg>dir
 Il volume nell'unità C è OS
 Numero di serie del volume: 9414-E1F8

 Directory di C:\Projects\edi-sta\src\mainPkg

12/02/2015  14:18    <DIR>          .
12/02/2015  14:18    <DIR>          ..
12/02/2015  14:13             1.337 Main.class
12/02/2015  14:05             1.285 Main.java
11/02/2015  11:01         3.692.096 ojdbc6.jar
               3 File      3.694.718 byte
               2 Directory   4.861.566.976 byte disponibili

The Main.class file is created performing this command:

javac -cp ojdbc6.jar;. Main.java

I used also here the ; separator because I am under Windows.

Now that I have create the Main.class file I try to perform this operation:

java -cp ojdbc6.jar;. Main

but I still obtain the "impossible to find or load main class" error message:

C:\Projects\edi-sta\src\mainPkg>javac -cp ojdbc6.jar;. Main.java

C:\Projects\edi-sta\src\mainPkg>java -cp ojdbc6.jar;. Main
Errore: impossibile trovare o caricare la classe principale Main

C:\Projects\edi-sta\src\mainPkg>

What am I missing? What is wrong?

Tnx

2 Answers 2

3

You also need the jar in the classpath at runtime:

java -cp ojdbc6.jar:. Main

Otherwise, obviously, the JVM can't load the classes from the jar file: where would it find them?

Note that you should never use the default package. Get into the good habit of defining a package for all your classes.

Sign up to request clarification or add additional context in comments.

6 Comments

I tryied to do as you explained but now I obtain the "impossible to find or load main class". What could be the problem?
Are you on Windows or Linux? what is the content of the current directory (i.e. what is listed when you execute ls on Linux or dir on Windows)?
I am using Windows. The directory contains the Main.class, Main.java and the ojdbc6.jar files
On Windows, the separator is ;, not :. So the command is java -cp ojdbc6.jar;. Main.
Is your class in the package mainPkg? If so, you should be compiling and running from the src directory. To compile: javac -cp mainPkg\ojdbc6.jar mainPkg\Main.java. To run: java -cp mainPkg\ojdbc6.jar;. mainPkg.Main. But jar files shouldn't be in the source directories. And you should use the -d option to specify a directory other than the current one for class files. The classpath must contain jar files and directories that constitute the root of the package tree.
|
1

You need the external JAR in the CLASSPATH at runtime, too. Add the same -cp argument when you run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.