I am trying to execute a java compiled file from another java program, and I am having some issues.
When I run from my terminal the command java -cp ".:lib/MyLib.jar" javaFiles/g1/MyCompiledProgram I can execute MyCompiledProgram without any issue.
But when I try to execute the same command from code using the following method:
Process process = Runtime.getRuntime().exec(String.format("java -cp \".:%s\" javaFiles/g1/MyCompiledProgram",Path.of(PATH_MYLIB)));
String error = null;
process.waitFor();
if(process.exitValue() != 0){
try(Scanner scanner = new Scanner(process.getErrorStream())){
error = scanner.useDelimiter("\\A").next();
}
System.out.println(error);
}
I get a ClassNotFoundException error. I checked that the directory java was using to execute the command was correct (running the pwd command) and it is the correct one.
Does anyone has any idea why is not finding the class? Thanks :)
Related
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.
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 am trying to run a python script from a java bean on a Tomcat server. The code that executes this looks like this:
Process child = Runtime.getRuntime().exec(test);
BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
String line = in.readLine();
String response = "";
while(line != null){
response += line;
line += in.readLine();
}
child.waitFor();
The error I receive is in the form of two pop-ups one that reads "Could not determine the package or source package name." and the other is an OS error from Ubuntu stating an internal error has occurred with the python script.
Is there something in Tomcat that needs to be setup in order for it to be able to execute a python script on the local system?
Some background, I need the python script to dynamically generate an image based on data selected by the user. My test system is Ubuntu 12.04, I will be deploying on a CentOS system.
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" });