How to run a .jar file from within Java program?

15,865

Solution 1

Use an absolute path instead of a relative path, that should prevent the path not being found if you run from any working directory. Otherwise add it to your classpath as Nizil said.

To get the current user's name, use System.getProperty("user.name") and concatenate into your path.

user = System.getProperty("user.name");
cmd = "java -jar C/Users/" + user + "/appdata/Roaming/<folder>/<file>.jar";
Runtime.getRuntime().exec(cmd);

Solution 2

You just have to add the jar's path in your classpath (don't forget to use an absolute path), and call the main method of the jar in you code. However, this solution is user-specific, as the path will be harcoded, unless you want to dive into something more tricky (How do you change the classpath within java).

Share:
15,865
user2098268
Author by

user2098268

Updated on June 04, 2022

Comments

  • user2098268
    user2098268 almost 2 years

    What I'm basically trying to do here, is run a .jar file which is located under

    C/Users/-any user here-/appdata/Roaming/-my folder here-/-file name here-.jar
    

    Do I somehow open a CMD and do:

    cd appdata/Roaming/<Folder>
    java -jar <FileName>.jar
    

    This seems to work when I type it into CMD itself. I can't seem to make it work when running from java program.

    I tried to do:

    Runtime.getRuntime().exec("cd appdata/Roaming");
    

    And I get an error that the specified directory doesn't exist.

    • Andrew Thompson
      Andrew Thompson almost 11 years
      A few tips. Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces.
    • Viktor Seifert
      Viktor Seifert almost 11 years
      @AndrewThompson This seems like an answer to me instead of just a comment.
    • Andrew Thompson
      Andrew Thompson almost 11 years
      @ViktorSeifert If the question had not already been offered a (correct) answer I might have submitted it as one. Of course, it might become the answer to the next few questions posted by the OP. ;)
  • user2098268
    user2098268 almost 11 years
    Ow boy.. That's why I typed out what I need. I'm making a Minecraft like launcher, which will launch my game which is located in appdata/Roaming from anywhere on the computer.
  • user2098268
    user2098268 almost 11 years
    How do I know what the user's name is?
  • CrystalDuck
    CrystalDuck almost 11 years
    Concatenate with a variable? You can't use a wildcard to search for users, because then how do you know which user to take if there are multiple users?
  • Viktor Seifert
    Viktor Seifert almost 11 years
    @user2098268 See the accepted answer in stackoverflow.com/questions/797549/get-login-username-in-jav‌​a to get the username.
  • CrystalDuck
    CrystalDuck almost 11 years
    I'll add that here. Thanks @ViktorSeifert
  • Viktor Seifert
    Viktor Seifert almost 11 years
    Also I should mention that cd-ing from a running process doesn't mean that the working directory will change for subsequent calls to Runtime.getRuntime().exec(...) . This is just one more reason to use the absolute path.
  • Viktor Seifert
    Viktor Seifert almost 11 years
    You still have the cd command in there. I think it should be java -jar since you have the jar at the end of the command.
  • CrystalDuck
    CrystalDuck almost 11 years
    @ViktorSeifert Correct, my bad. It was only to illustrate the principle but java -jar is indeed what the asker was looking for and so may be more appropriate.
  • user2098268
    user2098268 almost 11 years
    Thanks for code I will try it. If it works I will set this as the best answer.
  • user2098268
    user2098268 almost 11 years
    Thank you very much. Couldn't be a better answer :)
  • NiziL
    NiziL almost 9 years
    Well, if you use an absolute path, it will. But only on your computer, for your user ;)