Results 1 to 13 of 13
Like Tree2Likes
  • 2 Post By Norm

Thread: Compiling a Jar w/ a external lib

  1. #1
    ateixeira is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Exclamation 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):

    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());
            }
      
        }
    And I suspect the problem is over here, more precisely in here:
    "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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Compiling a Jar w/ a external lib

    throw new Exception("DB connection error "+e.getMessage());
    Its better to get the full text of the error message. Add:
    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

  3. #3
    ateixeira is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Compiling a Jar w/ a external lib

    There is a line in the manifest file where you can specify the classpath: Class-Path:

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Compiling a Jar w/ a external lib

    Quote Originally Posted by ateixeira View Post
    jar cfmv0 Depositos.jar MANIFEST.MF Depositos.class C:\oracle\ora92\jdbc\lib\classes12.jar
    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.

  6. #6
    ateixeira is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Compiling a Jar w/ a external lib

    Quote Originally Posted by ateixeira View Post
    Hello again thx for the replies,

    I've used this command now:
    jar cfmv0 Depositos.jar MANIFEST.MF Depositos.class .\lib\classes12.zip
    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.

    Quote Originally Posted by ateixeira View Post
    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
    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.

    Quote Originally Posted by ateixeira View Post
    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)
    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.

    Quote Originally Posted by ateixeira View Post
    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
    You really need to put printStackTrace() in your exception handling.

    Quote Originally Posted by ateixeira View Post
    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.
    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.

  8. #8
    ateixeira is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Compiling a Jar w/ a external lib

    Thx for the quick reply

    Quote Originally Posted by Tolls View Post
    Since you have supplied the manifest with all the correct details in it you want to use:
    java -jar Depositos.jar
    That's all.
    I've Tried that, and the result was:
    Exception in thread "main" java.lang.NoClassDefFoundError: Depositos/Depositos

    Quote Originally Posted by Tolls View Post
    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.
    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.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default 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

  10. #10
    ateixeira is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Compiling a Jar w/ a external lib

    Quote Originally Posted by Norm View Post
    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.
    yup it's there, the jar content is

    META-INF\MANIFEST.MF
    Depositos.class

    Quote Originally Posted by Norm View Post
    You must have done it once because your code needed to start executing to get: Exception: DB connection error oracle.jdbc.driver.OracleDriver
    It doesn't make sense I know... it's so frustrating...

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default 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.
    Tolls and ateixeira like this.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default 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.

  13. #13
    ateixeira is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Compiling a Jar w/ a external lib

    yup, that really was the problem. It's working now, thanks for the help guys.

Similar Threads

  1. About compiling
    By edh in forum Advanced Java
    Replies: 2
    Last Post: 06-16-2011, 09:47 AM
  2. Serversocket for external IP
    By Knetic in forum Networking
    Replies: 2
    Last Post: 02-16-2011, 02:32 PM
  3. how to get compiling
    By philgrek in forum New To Java
    Replies: 1
    Last Post: 05-25-2010, 11:53 PM
  4. External DTD requirements
    By jwilley44 in forum XML
    Replies: 0
    Last Post: 03-06-2009, 09:25 PM
  5. How to execute external EXE?
    By 2bGeek in forum AWT / Swing
    Replies: 3
    Last Post: 02-21-2009, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •