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 3 of 3

Thread: How to execute an executable JAR from within Java program?

  1. #1
    Junior Member Hawke's Avatar
    Join Date
    Jun 2013
    Location
    United States
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to execute an executable JAR from within Java program?

    Don't know if this would belong in I/O, sorry if it doesn't.

    I have a game launcher program and I need it to launch my game after patching when I click on a button. I want to be able to execute this game (which is java based) within the program instead of using .bat files because I want a solution that works cross platform instead of having operating system specific commands.

    If there is no such method could you please list a method for Windows, Mac, and Linux so that I can use if statements and System.getProperty("os.name") to launch the jar that way?

    Thanks in Advance,
    Hawke


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to execute an executable JAR from within Java program?

    I think ProcessBuilder (ProcessBuilder (Java Platform SE 6)) is what you want. I've never used it myself, so I can't tell you for sure how to use it. You can probably find some helpful information by googling about it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member Hawke's Avatar
    Join Date
    Jun 2013
    Location
    United States
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to execute an executable JAR from within Java program?

    Thanks this worked for me:

         ProcessBuilder pb = new ProcessBuilder("java", "-jar", jarname);
         pb.directory(new File(jarname).getParentFile());
     
         try{
     
    	pb.start();
     
         }catch(IOException ex){
     
    	ex.printStackTrace();
         }

Similar Threads

  1. Creating a Jar executable java program
    By dougie1809 in forum Java SE APIs
    Replies: 14
    Last Post: March 21st, 2013, 09:19 PM
  2. No sound in executable jar
    By BestSanchez in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 25th, 2012, 06:01 PM
  3. .jar executable
    By mwr76 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 9th, 2011, 11:27 AM
  4. Jar Executable File
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 43
    Last Post: June 23rd, 2010, 03:53 PM
  5. Making executable JAR more "executable"
    By ni4ni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 01:19 PM