I adopted the code from one of the similar questions:
Process p = null;
ProcessBuilder pb = new ProcessBuilder("scr.sh");
pb.directory(new File("/Users/alex/"));
p = pb.start();
Thread.sleep(TimeConst.SECOND);
And run this code from public static main(), I did place scr.sh file under alex folder but receive the exception: Caused by: java.io.IOException: error=2, No such file or directory
What's wrong with my code?
I removed a line that specified a working directory and replace file name with a absolute path instead and then it worked.
In order to receive echo messages I had to read from my stdin (?):
final Scanner in = new Scanner(p.getInputStream());
new Thread(() -> {
while (in.hasNextLine())
System.out.println(in.nextLine());
}).start();
Related
Hey all I am trying to change directories and then run my command with parameters.
final String path = "\\Local// Apps\\IBM\\SDP\\scmtools\\eclipse";
final String command = "scm help";
final String dosCommand = "cmd /c \"" + path + "\"" + command;
final Process process = Runtime.getRuntime().exec(dosCommand);
final InputStream in = process.getInputStream();
int ch;
while((ch = in.read()) != -1) {
System.out.print((char)ch);
}
It runs without errors but outputs nothing. However, this is what shows up after it finishes:
<terminated, exit value: 0>C:\Local Apps\IBM\SDP\jdk\bin\javaw.exe (Jul 22, 2019, 11:21:37 AM)
The expected output should be:
So am I doing this correctly?
AS suggested by Andreas
Process p = null;
ProcessBuilder pb = new ProcessBuilder("scm.exe");
pb.directory(new File("C:/Local Apps/IBM/SDP/scmtools/eclipse"));
p = pb.start();
I get the following error:
Cannot run program "scm.exe" (in directory "C:\Local Apps\IBM\SDP\scmtools\eclipse"): CreateProcess error=2, The system cannot find the file specified
You should use ProcessBuilder instead of Runtime.exec, e.g.
Process proc = new ProcessBuilder("scm.exe", "help")
.directory(new File("C:\\Local Apps\\IBM\\SDP\\scmtools\\eclipse"))
.inheritIO()
.start();
proc.waitFor(); // optional
You can also go through the command interpreter if needed, e.g. if the command is a script (.bat or .cmd file):
Process proc = new ProcessBuilder("cmd", "/c", "scm", "help")
.directory(new File("C:\\Local Apps\\IBM\\SDP\\scmtools\\eclipse"))
.inheritIO()
.start();
proc.waitFor();
The inheritIO() means that you don't need to process the commands output. It will be sent to the console, or wherever Java's own output would go.
I am trying to run a .jar from within my Java program. I am using ProcessBuilder to do so, but it is not working correctly.
I am wondering if I am missing something.
This is what I currently have that is trying to run the .jar
ProcessBuilder pb = new ProcessBuilder("java", "-jar", System.getProperty("user.home") + "/JARFile/JARFile.jar");
Process p = pb.start();
I have the directory correct, so I am not positive why this is not working properly.
Do I have something wrong with my parameters in the new ProcessBuilder?
1) in third argument set full path to file:
ProcessBuilder pb = new ProcessBuilder("java", "-jar",
"/home/meiskalt7/Documents/runJar-55056616-1.0-SNAPSHOT.jar");
Result will be look like this:
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("java", "-jar",
"/home/meiskalt7/Documents/runJar-55056616-1.0-SNAPSHOT.jar");
Process p = pb.start();
InputStream in = p.getInputStream();
System.out.println(new BufferedReader(new InputStreamReader(in))
.lines().collect(Collectors.joining("\n")));
}
and in console you will see result of execution
2) If everything will be good then you must check your system property with
System.out.println(System.getProperty("user.home"))
and if path looks like path in first step then you must compare path with equals operator:
System.out.println((System.getProperty("user.home") + "/JARFile/JARFile.jar")
.equals([YOUR FULL PATH]))
Maybe your problem with symbols of another language in path
2*) if something go wrong then you can check error of process execution in error stream of your process:
InputStream err = p.getErrorStream();
System.out.println(new BufferedReader(new InputStreamReader(err))
.lines().collect(Collectors.joining("\n")));
I have a project that uses ProcessBuilder to capture the output of the command "java -jar someJar.jar -argument", but have now moved the jar's source files to a separate package; somepackage. The package has a main function, so I would like to create a ProcessBuilder that captures the output of that process, as if it were a different Thread.
Is this possible, or will I have to completely re-write the code to allow it to use the source files instead of the binary?
If I'm assuming right, the package has main function and we are trying to get output of the main method that executes with java -jar processbuilder command.
ProcessBuilder pb = new ProcessBuilder(your java -jar command);
Process process = pb .start();
process.waitFor();
BufferedInputStream in = new BufferedInputStream(process.getInputStream());
byte[] contents = new byte[1024];
int jwtOytputBytesRead = 0;
String Output = "";
while ((jwtOytputBytesRead = in.read(contents)) != -1) {
Output += new String(contents, 0, jwtOytputBytesRead);
}
System.out.println(Output);
Try this link as well to specify the main the class
Run class in Jar file
I need to start a server using bash, so I had created an UNIX shell , but I am not able to execute it with Java from Eclipse.
I tried the following code which doesn't work :
Process proc = Runtime.getRuntime().exec(./startServer);
Here is content of the startServer file :
#!/bin/bash
cd /Users/sujitsoni/Documents/bet/client
npm start
You can try the following two options.
Option 1
Process proc = Runtime.getRuntime().exec("/bin/bash", "-c", "<Abosulte Path>/startServer");
Option 2
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "<Absolute Path>/startServer");
pb.directory(new File("<Absolute Path>"));
Process proc = pb.start();
A couple Of things can go wrong:
The path to the file you have given might be wrong for eclipse it can take relative path but from the command line, it will take the absolute path.
error=13, Permission denied - If the script file doesn't have required permissions. In your scenario, that might not the case as you are not getting any error.
At last, you are executing the script by java program so the output of your script will not be printed out. In your scenario, this might be the case. You need to capture the output of script from BufferedReade and print it. ( In your case server might have started but you are not seeing the logs/output of the script.
See the code sample below for printing output.
public static void main(String[] args) throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec("./startServer");
proc.waitFor();
StringBuffer output = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
System.out.println(output);
}
I want to unzip tgz files using 7zip utility in Windows OS. My 7zip.exe file is placed in D: drive. My unzip command is 7z e abc.tgz. This command is working through terminal but not through my java code.
Previously I tried
Runtime r=Runtime.getRuntime();
r.exec("cmd.exe d: ", null, new File("D:\\"));
Process p=r.exec("7z e abc.tgz"null,new File("D:\\"));
InputStream is=p.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line=new String();
while ((line=br.readLine())!=null) System.out.println (line);
I even tried this
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","start","cmd");
pb = pb.directory(new File("D:"));
pb.command("7z", "e", "abc.tgz");
pb.start();
Is there any different method for changing the directory through java?
You can use this snippet to change the working directory:
String[] command = unzip_your_file;
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File("directory_location"));