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/
java -jar .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.
解决方案
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//.jar";
Runtime.getRuntime().exec(cmd);