This question already has answers here:
CreateProcess error=2, The system cannot find the file specified
(6 answers)
Closed 5 years ago.
Heres my code please help! I'm making a launcher for my game. I want to be able to launch the game from the launcher but it dosent work.
ProcessBuilder pb = new ProcessBuilder("test.jar","C:/Users/Marcus/Documents/");
try {
pb.directory(new File("C:\\"));
Process p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
error:
java.io.IOException: Cannot run program "test.jar" (in directory "C:\"): CreateProcess error=2, Cant find the file at java.lang.ProcessBuilder.start(Unknown Source)
Your test.jar is relative that's why i cannot find the it.
You have to specify the path to test.jar
Then you should check if your jar is executable. If not you will need to add java to the list of arguments when creating the ProcessBuilder.
Related
This question already has answers here:
Executing a shell script inside a jar file. How to extract?
(4 answers)
Closed 2 years ago.
I've created a .jar in which I have a script named init.sh
I got this error.
tim#TS:~/Desktop$ java -jar test.jar
java.io.IOException: Cannot run program "/init.sh": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:621)
at java.lang.Runtime.exec(Runtime.java:486)
at Main.run(Main.java:42)
My code which works when I run on Eclipse is below.
String[] cmd = {"src/init.sh"};
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I've tried to change it without success like that.
String[] cmd = {"init.sh"};
I looked at my .jar file and the init.sh script is at the same level as the Main.class.
My question is : How must I change my code if I want to get a functional .jar ?
Based on the answer here Executing a shell script inside a jar file. How to extract?, looks like the shell script needs to be extracted to a location outside the jar file before you can execute it.
Recently, I have switched to Ubuntu v20.04 from Windows 10 Pro v2004 because of performance purposes. When, I was on Windows I can freely compile a java project from another java program by writing:
String pathToCompiler = "\"C:/Program Files/Java/jdk-14/bin/javac\"";
Process compileProcess = Runtime.getRuntime().exec(pathToCompiler+" -d bin #.sources", null, new File("ProjectPath"))
Where the sources file is a file containing the list of classes of the project
The code above works successfully on Windows 10.
But On Linux(Ubuntu):
if I substitute the value of variable pathToCompiler as
pathToCompiler = "\"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac\""
the below exception is raised up and the program executing the command exits:
"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac" -d bin #.sources
java.io.IOException: Cannot run program ""/usr/lib/jvm/java-11-openjdk-amd64/bin/javac"" (in directory "/home/arham/Documents/Omega Projects/Project0"): error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at java.base/java.lang.Runtime.exec(Runtime.java:592)
at java.base/java.lang.Runtime.exec(Runtime.java:416)
at ide.utils.systems.BuildView.lambda$3(BuildView.java:267)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
The problem is that the file actually exists but it says No Such File or Directory
Actually, The program which is compiling the project is a Java IDE that I am creatiing.
Someone please tell if he/she knows how to fix this bug
The Runtime.exec method has several problems that make it difficult to use, and this is one of them. Use the newer ProcessBuilder class instead.
String pathToCompiler = "C:/Program Files/Java/jdk-14/bin/javac";
Process compileProcess = new ProcessBuilder(pathToCompiler, "-d", "bin", "#.sources")
.directory(new File("ProjectPath"))
.start();
The differences are:
Remove the extra quotes from around the path to the executable. If quoting is needed, the system takes care of it.
Pass the each command line arguments as a separate string. This way you don't have to worry about quoting.
Update the path to the following:
String pathToCompiler = "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac/";
I'm currently working on invoking bash program using java. The bash program output too much message and I want to redirect them to /dev/null. But I encountered a weird error No such file or directory.
Here is my demo.
public static void main(String[] args) {
try {
// Another version I've tried:
// Process p = Runtime.getRuntime().exec("echo a > /dev/null");
ProcessBuilder b = new ProcessBuilder("echo a");
// b.redirectOutput(new File("/dev/null")).redirectErrorStream(true);
b.redirectOutput(ProcessBuilder.Redirect.to(new File("/dev/null")))
.redirectErrorStream(true);
Process p = b.start();
p.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
And the error message as follows:
java.io.IOException: Cannot run program "echo a": error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at test.main(test.java:12)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
... 2 more
I'm using a MacBook with Catalina, and I tried java 1.8.0_231 and 1.8.0_241 from oracle. (I couldn't use higher java version because one of the dependency of my project requires java 8).
To ignore the output from the process, it's easier and more portable to use ProcessBuilder.Redirect.DISCARD than explicitly redirecting to a special file/device such as /dev/null.
b.redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectErrorStream(true);
Forget about using Runtime.exec - that method is badly designed and hard to use safely. If you want to do input redirection with the "> /dev/null" style, you need to remember that > is a construct created by the command interpreter shell, not the operating system, and if you want to use it you must run a shell.
Runtime.getRuntime().exec(new String[] {"sh", "-c", "echo a > /dev/null"});
I am a novice at programming. I have recently tried to download the source code of an open source software and setup the environment. However, I am seeing the following issue when I tried to run the build for the first time:
Execute failed: java.io.IOException: Cannot run program "unzip" (in directory "....\"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start
Any tips on how to resolve this issue?
From what I understand, it is unable to a file:
where exactly to look for ProcessBuilder.start
How do I modify it?
Try this:
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("Write command here").start();
Edit: Command - string array which begin with fullpath to your programm. For example:
processBuilder.command("C:\\Program Files (x86)\\Microsoft Office\\Office15\\OUTLOOK.exe").start();
I have an exe file: "TVRRun.exe" that is located in the directory: "C:\Program Files (x86)\TeleVantage\Client\Reporter".
I'm trying to run that exe file from java code:
public static void runTVRRun() throws IOException {
String path = System.getenv("programfiles(x86)") + "\\TeleVantage\\Client\\Reporter\\";
String[] command = {"TVRRun.exe", "-S", templatePath, "-S", tmpReportPath};
Runtime.getRuntime().exec(command, null, new File(path));
}
but I get the following error:
Exception in thread "main" java.io.IOException:
Cannot run program "TVRRun.exe" (in directory "C:\Program Files (x86)\TeleVantage\Client\Reporter"):
CreateProcess error=2, The system cannot find the file specified
I've tried all variations of the exec function, and also ProcessBuilder, but the same error appears.