Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Running jar with libraries from a jar file in linux

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Running jar with libraries from a jar file in linux

    I'm running a jar of an application and library jars from lwjgl. I'm creating a java process. It's working on Windows but not on linux. I tried replacing the semicolons with colons but it didn't work, it just said "Main class not found: Application.natives.linux" or something like that. gamePath is the path the application is in. osName is the name of the operating system.

    Here's my code:
        String jarRunner = String.format("java -Djava.library.path=\"%snatives/" + osName + "\" -cp \"%sjar/lwjgl.jar;%sjar/lwjgl_util.jar;%sjar/jinput.jar;%sjar/slick.jar;%sApplication.jar\" main.Main", gamePath, gamePath, gamePath, gamePath, gamePath, gamePath);
     
     
        Process p = Runtime.getRuntime().exec(jarRunner);
     
        			InputStream s = p.getErrorStream();
     
        			while(true)
        			{
        				if(s.available() > 0)
        				{
        					byte[] bytes = new byte[s.available()];
        					s.read(bytes);
        					System.out.println(new String(bytes));
        				}
        			}

    How can I make it work on linux?


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Running jar with libraries from a jar file in linux

    You need to use colons (:) for the -cp argument.

    Print jarRunner to System.out to see its final form. Copy the printout and run it on command line confirm that it has been constructed properly. If it is confirmed, then you may need to use string array variant of the exec() method.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Running jar with libraries from a jar file in linux

    I'm using colons, but I don't know how to convert this into an array for the exec method.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Running jar with libraries from a jar file in linux

    If you have a command, "/path/to/bin/java -cp /path/to/jars pkg.MainClass", break it up into separate strings as delimited by the space characters in the command, and assemble the strings into an array. E.g.,
    String[] cmd = new String[] {"/path/to/bin/java", "-cp", "/path/to/jars", "pkg.MainClass"};
    Process p = Runtime.getRuntime().exec(cmd);

    Before you do that, repeating from my previous post,
    Quote Originally Posted by jashburn View Post
    Print jarRunner to System.out to see its final form. Copy the printout and run it on command line confirm that it has been constructed properly.
    In other words, are you 100% the string referenced by jarRunner has been correctly constructed for Linux? Try to run the command as constructed for jarRunner on the command line first.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Running jar with libraries from a jar file in linux

    Now the syntax itself seems to be working. But it's not finding the main class when using -cp or can't acess the jar file when using -jar.

  6. #6
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Running jar with libraries from a jar file in linux

    Please clarify what you mean by "syntax itself seems to be working." How is it that it is "working"? By "it's not finding the main class," do you mean if you type/paste the command that is constructed in jarRunner into the command line console, you an error message similar to the message in your first post? If there's an error message, please post here the error message in its entirety.

    Please also post here the command that is constructed in jarRunner.

Similar Threads

  1. Problem running jar file
    By zerogmd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2013, 02:12 PM
  2. Running .JAR file issue.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 27th, 2013, 06:07 AM
  3. Running an Applet With JAR file
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2011, 07:10 AM
  4. [SOLVED] jar file Built(clean and build) perfectly, but not running as jar
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: July 11th, 2011, 11:41 AM
  5. jar file works on XP but not on Linux
    By cl2606 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 10th, 2011, 09:19 AM

Tags for this Thread