Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By pbrockway2

Thread: Executing a jar with multiple libraries

  1. #1
    jpurdy647 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

    Default Executing a jar with multiple libraries

    I am not necessarily 'new' to java, however I have always used NetBeans so I am not entirely sure how to do what I need with the terminal commands. I am trying to execute a java jar file(which works perfectly on windows) on the Raspberri Pi, essentially debian linux.

    As I mentioned previously, I have a java jar file which I can execute on windows, but it depends on three other libraries, namely RXTX, MySQL, and JSON. I am not positive how to compile this into a jar, or execute it in debian.

    This was how I attempted to load two of those libraries, however the last line is the error I recieved.

    Direct copy from the terminal:
    pi@raspberrypi ~/Desktop $ java -Djava.library.path=/usr/lib/jni -cp /usr/share/java/RXTXcomm.jar:/usr/share/java/mysql-connector-java-5.1.10.jar:. Automation_System.core.HAS
    Error: Could not find or load main class Automation_System.core.HAS
    Specifically, the problem seems to be that I cannot use the -cp flag to indicate classpaths, and the -jar flag indicating I am executing a jar in the same command. I am not sure how to point the compiler to my main class HAS in the core package of the jar, and also to the libraries referenced.
    Last edited by jpurdy647; 05-16-2015 at 09:22 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Executing a jar with multiple libraries

    If I understand you correctly the main application is already in a jar and you're having trouble trying to set the classpath when you try to run it with -jar.

    It is correct that the -cp flag does nothing when you run a jar with -jar. Jar files manage their own classpath through the manifest file that is inside the jar; if you had to manage the classpath yourself, you'd not be able to run applications by double clicking the jar.

    Open up the jar file in a zip tool and open up the META-INF/manifest.mf file in a text editor. Is there a line in there starting with class-path: ?

    Recommended reading:

    https://docs.oracle.com/javase/tutorial/deployment/jar/
    jpurdy647 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jpurdy647 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

    Default Re: Executing a jar with multiple libraries

    I found the classpath you mentioned, and that seemed like it would definitely solve it. I modifed the classfile to this:

    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.9.4
    Created-By: 1.8.0_31-b13 (Oracle Corporation)
    Class-Path: /usr/share/java/mysql-connector-java-5.1.10.jar lib/json-simple-1.
    1.1.jar /usr/share/java/RXTXcomm.jar
    X-COMMENT: Main-Class will be added automatically by build
    Main-Class: core.HAS
    When executing the jar on the debian system though, I recieved this error:
    pi@raspberrypi ~/Desktop $ java -jar Automation_System.jar
    java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java :1865)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1119)
    at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentif ier.java:123)
    at core.SerialInterface.Connect(SerialInterface.java: 155)
    at core.SerialInterface.<init>(SerialInterface.java:8 2)
    at core.HAS.main(HAS.java:30)
    I am pretty sure rxtxSerial refers to a dll or the linux equivalent. That is on the system though, from having installed the rxtx package. I can run a test class(not jar) which uses the rxtx library just fine. The problems come along when I use a jar. Also, thank you for the link. I will read that.
    Last edited by jpurdy647; 05-16-2015 at 11:46 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Executing a jar with multiple libraries

    I am pretty sure rxtxSerial refers to a dll or the linux equivalent.
    That sounds right to me.

    The jar archive will take care of the path - the place where the runtime will look for classes - with the manifest entry. But I think you have to tell it the value of the java.library.path value. This is where native libraries will be looked for. The java command has a -D flag for this purpose:

    Java Code:
    java -jar -Djava.library.path=/your/path/here/ yourJar.jar
    You will have to determine the path to use! It's likely to be a lib folder. The "man" command might tell you where the software puts it shared library files. And the thing you are loading isn't a "dll" anymore, but might be named something like "librxtxSerialso".
    jpurdy647 likes this.

  5. #5
    jpurdy647 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

    Default Re: Executing a jar with multiple libraries

    I added the path when executing the jar file and it ran and communicated succesfully! Thank you both!

    It is now failing with an sql error, but that may be because the mysql server is not running, or some other reason. This should be the last hurdle, thanks again!

    Edit: The problem seems to be that I have not yet included the mysql database connector 'dll's as I did with the java.library.path for the rxtx library. Do you know where the mysql native libraries are stored?

    This was the error:
    java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/climate
    at java.sql.DriverManager.getConnection(DriverManager .java:689)
    at java.sql.DriverManager.getConnection(DriverManager .java:247)
    at core.DHTManager.<init>(DHTManager.java:41)
    at core.HAS.main(HAS.java:39)
    Last edited by jpurdy647; 05-17-2015 at 01:09 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Executing a jar with multiple libraries

    Maybe the Installing the Driver and Configuring the CLASSPATH page of the dev.mysql.com documentation will help. That suggests that it is the classpath (ie a jar archive) not a dll or other library that is being looked for.

    [Edit] That page talks about the -cp switch but, of course, in your case it will be your jar file's manifest that ought to include the path.

  7. #7
    jpurdy647 is offline Member
    Join Date
    May 2015
    Posts
    4
    Rep Power
    0

    Default Re: Executing a jar with multiple libraries

    After long hours of searching for a different din.jar file, I discovered I was pointing to the wrong jar in my classpath. There are two mysql jars:



    I was using the one with the version number, which was causing the problem. Apparently you are supposed to point the class path to the 'mysql-connector-java.jar' instead.

    My server is running as it should now. Thank you so much for your help!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Executing a jar with multiple libraries

    You're welcome. I'm glad you've got it sorted out.

Similar Threads

  1. Netbeans hangs while exit after completing executing for multiple thread
    By Neha Sharma in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-28-2013, 09:23 PM
  2. How to use libraries?
    By Josep_16 in forum New To Java
    Replies: 1
    Last Post: 09-06-2011, 01:50 PM
  3. Replies: 2
    Last Post: 06-26-2010, 04:39 PM
  4. Replies: 1
    Last Post: 11-04-2009, 02:45 AM
  5. Replies: 2
    Last Post: 07-18-2008, 01:13 PM

Posting Permissions

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