Processbuilder in Java is not throwing up the sub process exception - java

I am trying to execute a jar file in Java using ProcessBuilder. Now, Whenever the sub process called by the ProcessBuilder, throws any exception, the ProcessBuilder don't catch the exception and execution keeps on proceeding continuously.
Below is my code:
try {
ProcessBuilder pb = new ProcessBuilder("java", "-jar", CommonConstants.jarFileLocation,
fileEntry.getAbsolutePath(), CommonConstants.commonFileLocation);
Process p = pb.start();
}
catch (Exception e) {
e.printStackTrace();
}
The catch block is suppose to print any exception whenever the sub process throws any. However, it never does.
Am i missing something?

Done it for printing in console....
I used the ProcessBuilder inheritIO method which sets the source and destination for sub process standard I/O to be the same as those of the current Java process.
Hence the modified code...
try {
ProcessBuilder pb = new ProcessBuilder("java", "-jar", CommonConstants.jarFileLocation,
fileEntry.getAbsolutePath(), CommonConstants.commonFileLocation).redirectError(Redirect.INHERIT);
Process p = pb.start();
}
catch (Exception e) {
e.printStackTrace();
}
However, need it to be caught by the catch block.
Need Help!

Related

How do I get the information of a batch script started in java, whether it has already ended

im calling multiple batch files in Java with the ProcessBuilder like
ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "Start", "batchTest.bat");
File dir = new File(path);
processBuilder.directory(dir);
try {
Process p = pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now i want the information that the script is done in java. Like just a sysout when the script is completly finished. process.isAlive() turns false before the script is done. And also process.witFor() doesnt get the job done. Maybe someone has an idea or a workaround?
Okay, i got it. process.waitFor() only works if you add the parameter "/wait" to the Constructor of the ProcessBuilder.
new ProcessBuilder("cmd", "/c", "Start", "/wait", "batchTest.bat");

Why can't "chgport.exe" be found by Runtime.getRuntime().exec() in Java?

In Java, I've tried to run:
Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\chgport.exe");
as well as
Process p = Runtime.getRuntime().exec("chgport.exe");
but getting the following Exception:
java.io.IOException: Cannot run program "C:\Windows\System32\chgport.exe": CreateProcess error=2, The system cannot find the file specified
I am using the NetBeans IDE and it is running with admin credentials.
I tried your code and its working fine, Try it like so:
String[] command = {"chgport"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("C:/Windows/System32/"));
pb.redirectErrorStream(true);
Process p = pb.start();
I tried both methods from Eclipse and both are working fine
Is it possible that you are not running your IDE with Administrator rights?
Can you try close the IDE and right click run as administrator?
try {
Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\mspaint.exe");
p.waitFor();
String[] command = {"mspaint"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("C:/Windows/System32/"));
pb.redirectErrorStream(true);
Process p2 = pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You could run it with CMD /C, which "carries out the command specified by string and then terminates".
Process p = Runtime.getRuntime().exec("CMD /C chgport.exe");

Collect environment variable in java after running script through ProcessBuilder

Why does the the following code print false ? I am trying to an environment variable in the test.sh script and collect it in java. Please suggest an alternative approach, if possible.
public static void main(String[] args){
ProcessBuilder processBuilder = new ProcessBuilder("test.sh");
Process process;
int exitCode;
try {
process = processBuilder.start();
exitCode = process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String>envVars = processBuilder.environment();
System.out.println(envVars.keySet().contains("SOURCE"));
}
And the code for test.sh script is simply
set SOURCE=source
The ProcessBuilder.environment() method is used for passing the initial environment to the process when you call start(). You cannot get the environment of a subprocess from a parent process. This is not a Java restriction: you can't even get a subprocess environment from a Bash shell script (or in fact anything) that creates a subprocess. You need to find another means of communicating information from the subprocess back to the parent process.
In my opinion, you should change:
ProcessBuilder processBuilder = new ProcessBuilder("test.sh");
to
ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "test.sh");
processBuilder.directory(new File(the-dir-of-test.sh));

How to set argv[0] name for ps when spawning program from Java's getRuntime().exec()?

How to set argv[0] name for ps when spawning program from java getRuntime().exec()?
Runtime rt = Runtime.getRuntime();
String[] cmd = {"/bin/sh", "-c", "ls > hello"};
rt.exec(cmd);
I want the process to have another name in ps ef output.
In C you can just overwrite argv[0] pointer, how to do it in java?
In Java you would need to create a symbolic link to the executable and call that instead. I don't believe you have access to the argv[0]
#user1335897: ProcessBuilder provides more flexibility than Runtime, try using following code:
ProcessBuilder processBuilder = new ProcessBuilder(args);
if (envMap != null) {
processBuilder.environment().putAll(envMap);
}
try {
Process process = processBuilder.start();
process.waitFor();
} catch (IOException ioe) {
} catch (InterruptedException ie) {
}

Running a batch file by java

I just wanted to run a batch file using java code in win7. I can run .exe files with the code but u know it doesn't work with a batch. Where is the problem? You know even cmd.exe doesn't start with that command. But I can run other exe files, I've tried some. The code is this (with try and catch is that): none of them worked!
Runtime.getRuntime().exec("cmd.exe /c demo.bat");
Runtime.getRuntime().exec("demo.bat");
i tried to do work with process and i wrote the code below. it retuened
java.lang.IllegalThreadStateException:process has not exited
at java.lang.ProcessImpl.exitValue(Native Method)
at Test.Asli.main(Asli.java:38)
this is the code:
try{
Runtime rt = Runtime.getRuntime();
Process proc= rt.exec("C:\\Windows\\System32\\cmd.exe");
int b = proc.exitValue();
// int exitVal = proc.exitValue();
//System.out.println("Process exitValue: " + exitVal);}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Try the following:
String[] cmd = {"cmd.exe", "/c", "demo.bat");
Runtime.getRuntime().exec(cmd);
I always prefer splitting the command and the parameters myself. Otherwise it is done by splitting on space which might not be what you want.
Try this:
Runtime.getRuntime().exec("cmd.exe /c start demo.bat");
Use this:
try {
Process p = Runtime.getRuntime().exec("C:PATH/TO/FILE/yourbatchfile.bat");
} catch(Exception e) {
e.printStackTrace();
}
It even hides the annoying prompt window (if you want that)

Categories

Resources