Results 1 to 13 of 13
- 09-12-2011, 06:21 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Compiling a Jar w/ a external lib
Hello,
Well I'm new to this forum so I'm sorry if I misinterpreted some of this forum rules feel free to point out any mistake, and for my English, since I'm not an English native speaker.
Now, although I program for several years, I've been forced to use an old version of JDeveloper on this project I'm in, and I've had some beginner issues, because I'm used to let the IDE do all the hard work.
My objective is, to compile my project to a jar and execute it in the server.
So far, I got my app to run and work on my IDE which is JDev 9.0.3.2 (OLD) and Java version is 1.3.1_01, but when I try to compile to a jar file for some reason the lib doesn't come along.
My code (partially ofc):
And I suspect the problem is over here, more precisely in here:Java Code:Connection con = null; public Depositos() throws Exception { System.out.println("Beginning ORACLE DB connection"); try { String connstr="jdbc:oracle:thin:"+"@<hostname>"+":"+"<port>"+":"+"<SID>"; System.out.println("connstr ok"); Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("Class.forName ok"); con = DriverManager.getConnection (connstr, "cic", "managercic"); System.out.println("Connection successful"); } catch (ClassNotFoundException e) { throw new Exception("DB connection error "+e.getMessage()); } }
"Class.forName("oracle.jdbc.driver.OracleDriver"); "
My procedure is:
On the Depositos.java folder
I run this:
javac -verbose Depositos.java -classpath C:\oracle\ora92\jdbc\lib\classes12.jar
the output says it's all OK.
jar cfmv0 Depositos.jar MANIFEST.MF Depositos.class C:\oracle\ora92\jdbc\lib\classes12.jar
my MANIFEST.MF contains:
Manifest-Version: 1.0
Created-By: Oracle JDeveloper 10.1.3.4.0
Main-Class: Depositos.Depositos
When I run the code from the src folder with:
java Depositos.Depositos
my output is:
Beginning ORACLE DB connection
connstr ok
Exception in thread "main" java.lang.Exception: DB connection error oracle.jdbc.driver.OracleDriver
at Depositos.Depositos.<init>(Depositos.java:47)
at Depositos.Depositos.main(Depositos.java:98)
Which leads me to the conclusion that the error is in the line I stated above.
And I don't understand what I did wrong, I've read so many post from other forums, I don't know what's the right move anymore.
I tried several combinations of compiling commands.
Thank you for your help.Last edited by ateixeira; 09-12-2011 at 07:04 PM.
- 09-12-2011, 06:59 PM #2
Re: Compiling a Jar w/ a external lib
Its better to get the full text of the error message. Add:throw new Exception("DB connection error "+e.getMessage());
e.printStackTrace();
to the catch block
You need to add the jar file with the missing class to the classpath. One way is to use the java -cp option:
java -cp <THEJARFILE>.jar;. oic.OIC
- 09-12-2011, 07:11 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Compiling a Jar w/ a external lib
Thank you for the quick reply, that did it for me, I have one other question tho, there's another way to include that <THEJARFILE> in the Depositos.jar so when I run it I don't have to use -cp <THEJARFILE>.jar;. ? And just do something like java -jar Depositos.jar
- 09-12-2011, 07:14 PM #4
Re: Compiling a Jar w/ a external lib
There is a line in the manifest file where you can specify the classpath: Class-Path:
- 09-13-2011, 10:14 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Compiling a Jar w/ a external lib
That's your problem.
You are attempting to add the classes12.jar to your jar file.
Jars should not contain other jars since they cannot reference them.
As Norm says, you should either run it using -cp etc or (prefereably) note the oracle jar in your manifest, and deploy the driver jar file with your app one.
- 09-13-2011, 06:19 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Compiling a Jar w/ a external lib
Hello again thx for the replies,
I've used this command now:
jar cfmv0 Depositos.jar MANIFEST.MF Depositos.class .\lib\classes12.zip
The content in the MANIFEST.MF file is (w/ the enter at the bottom)
Manifest-Version: 1.0
Created-By: Oracle JDeveloper 10.1.3.4.0
Main-Class: Depositos.Depositos
Class-Path: lib/classes12.zip
but still when I execute the jar file like this @ the src folder I have again (the same thing as before I used the command java -cp oic\lib\classes12.zip;. Depositos.Depositos)
Beginning ORACLE DB connection
connstr ok
Exception in thread "main" java.lang.Exception: DB connection error oracle.jdbc.driver.OracleDriver
at Depositos.Depositos.<init>(Depositos.java:47)
at Depositos.Depositos.main(Depositos.java:98)
command used:
java Depositos.Depositos
And one other thing, I tried to move the files like this to the server to do a test, and I've noticed I can't execute outside the directory where I've created the jar.
I moved the files to this folder:
/home/Me/Depositos/Depositos.jar
/home/Me/Depositos/lib/classes12.zip
And I went to /home/Me/ and use the command as it worked before > java -cp Depositos\lib\classes12.zip;. Depositos.Depositos
the result was Exception in thread "main" java.lang.NoClassDefFoundError: Depositos/Depositos
the server uses Linux, I've done the jar on windows, don't know if that helps.
I've tried a very large number of combinations seen throwout various forums and sites, but none seem to work for me, and I really like to understand the how.Last edited by ateixeira; 09-13-2011 at 06:22 PM.
- 09-13-2011, 06:30 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Compiling a Jar w/ a external lib
First off, you only jar up the class, not the zip. I told you earlier not to do that since jar (which this zip is) files cannot be referenced inside another jar.
That's correct there. That tells the JVM (when using java -jar) which is the main class, and where to find dependent jar files (in this case the classes12.zip) in relation to the jar file.
No.
Since you have supplied the manifest with all the correct details in it you want to use:
java -jar Depositos.jar
That's all.
You really need to put printStackTrace() in your exception handling.
It doesn't work because you didn't tell it where to find the jar file you are executing.
The classpath you gave it (the bit after -cp) only points to the classes12.zip.
- 09-13-2011, 07:03 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Compiling a Jar w/ a external lib
Thx for the quick reply
I've Tried that, and the result was:
Exception in thread "main" java.lang.NoClassDefFoundError: Depositos/Depositos
I've also tried > java -jar Depositos.jar -cp lib/classes12.zip
but that didn't work either...the result was the same as above.
- 09-13-2011, 07:08 PM #9
Re: Compiling a Jar w/ a external lib
Did you put the Depositos.class file in the Depositos folder inside of the jar file?
Look in the jar file with a zip utility to be see if that class file is in the Depositos folder.
You must have done it once because your code needed to start executing to get: Exception: DB connection error oracle.jdbc.driver.OracleDriver
- 09-13-2011, 07:14 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
- 09-13-2011, 07:16 PM #11
Re: Compiling a Jar w/ a external lib
You must put the Depositos.class file in the Depositos folder inside of the jar file since the class is in that package.
Your post shows that the class file is not in the folder.
- 09-14-2011, 09:17 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Compiling a Jar w/ a external lib
As Norm says, you are telling the JVM that your main class (the class with the main() method) is to be found in the Depositos package (though package names are supposed to start with a lower case), and is called Depositos.
This means the class will be found in a Depositos directory in the jar file.
If it is not to be found there then that is your first problem...you have created a duff jar file.
- 09-14-2011, 12:40 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
About compiling
By edh in forum Advanced JavaReplies: 2Last Post: 06-16-2011, 09:47 AM -
Serversocket for external IP
By Knetic in forum NetworkingReplies: 2Last Post: 02-16-2011, 02:32 PM -
how to get compiling
By philgrek in forum New To JavaReplies: 1Last Post: 05-25-2010, 11:53 PM -
External DTD requirements
By jwilley44 in forum XMLReplies: 0Last Post: 03-06-2009, 09:25 PM -
How to execute external EXE?
By 2bGeek in forum AWT / SwingReplies: 3Last Post: 02-21-2009, 12:59 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks