Is there any direct support (Asset) to run a batch file in jBPM.
I know that I can run Java code like below.
This is the java code I am trying to run.
ProcessBuilder pb = new ProcessBuilder("C:\\Files\\Test.bat");
Process p = pb.start();
int exitStatus = p.waitFor();
System.out.println("Execution Done. Status: "+exitStatus);
I am curious to if there is a direct way to run a Batch file directly without writing Java code manually.
I am not aware of how to execute the batch file but maybe you can try with
executing the shell script using work item handler.
Related
I am trying to run a batch file using java. The batch file in turn runs a python program. So i should wait till the batch file is done and then proceed with my program.
Problems facing:
I could not run batch file in background. I am able to run it only via start
Process p = Runtime.getRuntime().exec("cmd /c start c://GCTI//IA/QAART//testercheck.bat");
once the batch file ran, it is not closing automatically.
Batch file
"C:\Python27\python.exe" -i "C:\GCTI\IA\QAART\tester\test_monitor.py" -init "C:\GCTI\IA\EpiPhone\Dispatcher6\init\INIT_Designer_QAART_Dispatcher_Chat.PY" -testlist "C:\GCTI\IA\ASR_QAART\dat files\ChatAutomation\chat.dat" 23
Can you please help me to run this batch ile in background?
You don't need the batch file. You can execute the Python program directly from Java code using class java.lang.ProcessBuilder.
ProcessBuilder pb = new ProcessBuilder("C:\\Python27\\python.exe",
"-i",
"C:\\GCTI\\IA\QAART\\tester\\test_monitor.py",
"-init",
"C:\\GCTI\\IA\\EpiPhone\\Dispatcher6\\init\\INIT_Designer_QAART_Dispatcher_Chat.PY",
"-testlist",
"C:\\GCTI\\IA\\ASR_QAART\\dat files\\ChatAutomation\\chat.dat",
"23");
Process p = pb.start();
int result = p.waitFor();
Refer to other methods in class ProcessBuilder for handling the output of the Python script, for example method inheritIO
I am trying to loop into files, and run the script sh in each file using JAVA.
for (File file : files) {
if (file.isDirectory()) {
Runtime.getRuntime().exec("cmd.exe /c start "
+file.getAbsolutePath()+"\\creationNoPersisWF12.sh");
TimeUnit.SECONDS.sleep(1800);}}
The window of the script opens and get closed immediately.
However, when I try the execute the scripts sh from outside, they execute successfully.
I need some help please.
Process process = Runtime.getRuntime().exec("cmd.exe /c start "
+file.getAbsolutePath()+"\\creationNoPersisWF12.sh");
if(process.exitValue()!=0){
throw new RuntimeException(new String(process.getErrorStream().readAllBytes()));
}
you have to check the exit value and read the error stream for the errors since its a process on its own, it won't throw any error on its own after invoking the process you have to check if it ran successfully with exitvalue 0 or not.
I'm trying to execute a batch file and get the error code from it in Windows 7 Enterprise 64-bit.
My batch file is c:\test.cmd and contains a single line:-
exit 1
My code for executing the batch file is:-
public static void main(String[] args) throws Exception {
Process process = new ProcessBuilder("c:\\test.cmd").start();
System.out.println(process.waitFor());
}
The output is zero. If I try with:-
new String[] {"cmd", "/c", "c:\\test.cmd"}
the result is again zero.
There doesn't seem to be much magic to the ProcessBuilder API that I'm missing. Can anyone see where my code is going wrong?
Shouldn't I be able to capture the exit code of the batch file?
I think there is something wrong (or different) with my PC. The Apache Commons Exec project source code I downloaded failed unit tests when capturing return codes. Looks to be unsolvable on my PC and haven't found a workaround.
Through Jenkins - Slave setup (running in Windows), we have created a ANT job which in internally calls the below JAVA Program,
String[] command = {"cmd" , "/c", System.getProperty("user.dir")+"/Read_email/ReadEmail.vbs"};
Process p = Runtime.getRuntime().exec(command);
System.out.println("Process Completed");
The ReadEmail.vbs file never gets called or executed.
There is no error message or warning getting generated.
When I run this java program from eclipse or through Master Jenkinks, VB Scripts gets executed without any errors.
Your
String[] command = {"cmd" , "/c", System.getProperty("user.dir")+"/Read_email/ReadEmail.vbs"};
relies on the executing process to know where to find cmd.exe and who to call for a .vbs.
I used a 'fully redundant':
String[] command = {"C:/WINDOWS/system32/cmd.exe" , "/c", "C:/WINDOWS/system32/cscript.exe", "E:/trials/SoTrials/answers/21228622/java/callme.vbs"};
try {
Process p = Runtime.getRuntime().exec(command);
} catch(Exception e) {
System.out.format("%s\n", e.toString());
}
successfully from a simple commandline program. I hope this strategy works for your more complicated Jenkins setup.
I have a python compiled script (script.pyc , I haven't the .py file)that work well from my windows command prompt, and I want to execute it from my Java's application.
I tried to use runtime() method :
Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[] {"C:\\toto\\tools\\script.pyc" ,"arg","arg2" });
but I get an error :
Exception in thread "main" java.io.IOException: Cannot run program "C:\Nuance\VoCon Hybrid\SDK_v4_3\tools\clctodict.pyc": CreateProcess error=193, %1 n?est pas une application Win32 valid
The script work well in my terminal ("arg" is a txt file, "arg2" is the output name, and the script does its job without any problem).
I also try to launch my script with getDesktop() :
File fie = new File("C:\\toto\\tools\\script.pyc" ,"arg","arg2");
Desktop.getDesktop().open(fie);
There is no problem, but I can't add argument, so I can just see a terminal windows opening during a few second before disappearing instantly.
I have also tried to use JPython, without success too (maybe we can't use methode "execfile" on a .pyc????)
You can do something like
Process p = Runtime.getRuntime().exec(new String[]{"python.exe" ... other args)
Then you can invoke p.waitFor() to wait for the end of the process and p.exitValue() to test if the program exited successfully.
You can also get the output stream via p.getOutputStream() to retrieve the text printed by your python script
Please refer to the class documentation for further information : http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html
Just like you need a jvm to run a .class, you need a python interpreter to run a .pyc.
Try something like:
runtime.exec(new String[] {"c:\\Python26\\bin\\python.exe", "C:\\toto\\tools\\script.pyc" ,"arg","arg2" });