How to run a .jar file from within Java program?
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).
user2098268
Updated on June 04, 2022Comments
-
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 almost 11 yearsA 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 theProcess
using aProcessBuilder
. Also break aString arg
intoString[] args
to account for arguments which themselves contain spaces. -
Viktor Seifert almost 11 years@AndrewThompson This seems like an answer to me instead of just a comment.
-
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 almost 11 yearsOw 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 almost 11 yearsHow do I know what the user's name is?
-
CrystalDuck almost 11 yearsConcatenate 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 almost 11 years@user2098268 See the accepted answer in stackoverflow.com/questions/797549/get-login-username-in-java to get the username.
-
CrystalDuck almost 11 yearsI'll add that here. Thanks @ViktorSeifert
-
Viktor Seifert almost 11 yearsAlso I should mention that
cd
-ing from a running process doesn't mean that the working directory will change for subsequent calls toRuntime.getRuntime().exec(...)
. This is just one more reason to use the absolute path. -
Viktor Seifert almost 11 yearsYou still have the
cd
command in there. I think it should bejava -jar
since you have the jar at the end of the command. -
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 almost 11 yearsThanks for code I will try it. If it works I will set this as the best answer.
-
user2098268 almost 11 yearsThank you very much. Couldn't be a better answer :)
-
NiziL almost 9 yearsWell, if you use an absolute path, it will. But only on your computer, for your user ;)