AndreaNobili
AndreaNobili

Reputation: 43057

How correctly compile a Java class using a classpath?

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

Upvotes: 0

Views: 1514

Answers (2)

duffymo
duffymo

Reputation: 309008

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

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692151

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.

Upvotes: 3

Related Questions