Running batch file in java - java

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

Related

Execute a command file in jBPM

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.

How to know if a file is closed using java Process

I'm executing this command in order to open the log file with default file viewer:
Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler F:/Download/MSI3ca79.LOG");
I would like to know when the file is closed. Is it possible?
p.waitFor(); // doesn't work because the process is terminated just after the execution.
Solution:
ProcessBuilder pb =
new ProcessBuilder("c:/windows/notepad.exe", "F:/Download/MSI3ca79.LOG");
File log = new File(LogFactory.getLogFactory(TestExternProcess.class).getName()); pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
int exitVal = p.waitFor();
Plainly, it's not possible. I would not go this way.
It depends on a particular app (Notepad) opening file. The system doesn't know in general when an app stops viewing a file. Because what is closing a file? Is it closing a tab in a viewer UI? Or cleaning memory allocated for the file by an app? Removing lock file in case of closing doc, xls files?
In order to do it, you would need to write a program controlling the OS and any app that can view the file.

Run sh script with java but it fails without showing the error

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.

wget file download ftp waitfor() hangs

I am trying to download a XML file from a FTP server with wget in my Java programm.
I have to wait until it finishes the download.
String command = "WGET -O "
+props.getProperty("xmlFolder")+""+
+ rs.getString("software")
+ ".xml ftp://"+props.getProperty("ftpUser")
+":"+props.getProperty("ftpPasswort")+"#"+rs.getString("xmlPfad");
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("downloaded!");
Without the waitfor() it works perfectly, but with this function it stucks after 2-3 MB are downloaded. Any suggestions?
Have you tried to use the --quiet option for wget?
EDIT 1:
The pipe's write side (child process) might be full.
EDIT 2:
From openjdk-6-src-b20-21_jun_2010
In jdk/src/solaris/native/java/lang/UNIXProcess_md.c (at least for a UNIX system) we can see how Java launches a new child process and how it is using pipe to redirect stdout and stderr from child (wget) to parent process (Java)

Executing python compiled script (.pyc) in Java

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" });

Categories

Resources