How do i run jar files from within a java program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    How do i run jar files from within a java program?

    Hi,

    I have created several projects using eclipse and java. Each of these projects has a runnable .jar file. I would like to create one java program which would call each of my .jar files, kind of like a batch sequential system where one jar file would execute only after the previous one has completed.

    Code:
    public static void main(String[] args) {
         call jar file 1
         call jar file 2
         call jar file 3
    }
    this way, if i create future projects that need to run after my last jar file, i would simply add a new line:
    call jar file 4

    I have tried google but the examples there do not seem to be working on my machine. This is what i have tried:

    Code:
    ProcessBuilder builder = new ProcessBuilder("java -jar A.jar");
    Process process = builder.start();
    when i try to run this, i get an error:
    Cannot run program "java -jar A.jar": CreateProcess error=2, The system cannot find the file specified


    i have googled this error and this error refers to the environment path. I have verified this and my environment path points to the right location.

    any ideas?

    this is what's in my Environment Path:
    C:\Program Files\Java\jdk1 .6.0_25\jre\bin \;C:\Program Files\Java\jre6 \bin\;C:\Progra m Files (x86)\SSH Secure Shell;

    and this is whats in my class path:
    %SystemRoot%\sy stem32;%SystemR oot%;%SystemRoo t%\System32\Wbe m;%SYSTEMROOT%\ System32\Window sPowerShell\v1. 0\;C:\Program Files (x86)\Windows Live\Shared;C:\ Program Files\Java\jdk1 .6.0_25\jre\bin \;C:\Program Files\Java\jre6 \bin\;C:\Progra m Files\TortoiseS VN\bin;

    thank you!
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    A.jar should reside in current class location or it should be mentioned in the environment variables.

    Regards
    Dheeraj Joshi

    Comment

    Working...