I have a simple java file in linux. I am executing another java program HelloWorld from my Parent Java class.
If I keep the class file in current directory it workds but if i keep in another directory it wont work. could you tell me what mistake I am doing here?
Working:
theProcess = Runtime.getRuntime().exec("java HelloWorld");
Notworking Below:
theProcess = Runtime.getRuntime().exec("java -cp \"/home/sss/public_html/Project/WekaMLWorkbench/src/m85/\" HelloWorld");
Many Thanks.
I understood your problem. It happens because of the regnoition problem in linux and java based environments.
Use this method in Runtime.
exec(String[] cmdArray)
Substituting the values.
theProcess = Runtime.getRuntime().exec(new String[]{"java", "-cp" ,"/home/sss/public_html/Project/WekaMLWorkbench/src/m85/HelloWorld");
Related
From my application I have to execute an external jar of which I do not have the source.
Given an input file, it processes it, creates an "output" directory and puts in it an mxml output file. Problem is: it creates said directory in tomcat/bin instead of inside the directory of the original file.
Here's what I've tried so far.
Initially
Process p = new ProcessBuilder("java -jar "+newfile.getParent()+"\\converter.jar "+newfile.getPath()+" -mxml").start();
Then, seeing how from console the "output" directory was created in the directory the command was called from, I tried:
String startSim[] = {"cd "+newfile.getParent()+"\\" , "java -jar converter.jar "+newfile.getName()+" -mxml"};
try {
Runtime.getRuntime().exec(startSim).waitFor();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Log non creato.");
}
But with this I get the "file not found" exception for the first instruction. Does anyone know how to possibly solve this problem? I'd like to avoid having to reach for my output file all the way in my tomcat/bin directory.
Thanks for any suggestion!
Paolo
P.s.: by the way, before trying all this I tried simply calling the method I need from the library, but had the same exact problem. So I resolved to execute the jar, instead. And here we are. :)
You can set working directory using ProcessBuilder.directory() method:
ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File("mydirectory"));
pb.command(......);
etc
This does not work for you when you are using Runtime.exec() because cd command is a functionality of shell. You could solve it using this technique but you have to create platform specific command with prefix like cmd /c on windows or /bin/sh on Linux. This way is definitely not recommended.
But in your specific case you do not neither first nor second solution. Actually you are starting one java process from another. Why? you can easily invoke the main() method of the second process directly.
Take a look on META-INF/MANIFEST.mf file from converter.jar. Field Main-Class contains the fully qualified name of main class. Let's say it is com.converters.Main (just for example). In this case you can invoke
com.converters.Main.main(new String[] {newFile.getPath(), "-mxml"});
directly from your code. Just add the jar to your classpath.
Concerning to changing working directory in this case. Check again whether you really need this or your converters.jar supports parameter that does this.
A lazy approach to this may be going to the root directory and descending from there to your tomcat bin directory .
I am trying to compile a c program from a java program on Linux platform. My snippet is.
ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/gcc",
"-c","/hipad/UserProject/example.c");
Process proc = processBuilder.start();
There is no error during compilation of java program but I am not able to get .o file. I tried to find out solutions but no one is working.
Any suggestion.....
The default working directory of a child process is what ever directory the Java process has as a working directory, which usually is where it was launched from. And by default gcc writes output files to current working directory. That's where you should find example.o.
There are two simple ways to solve this. You can give gcc -o option and full path and name of desired output file, or you can set working directory of child process, like this:
ProcessBuilder processBuilder =
new ProcessBuilder("/usr/bin/gcc", "-c","example.c"); // source in working dir
processBuilder.directory(new File ("/hipad/UserProject")); // or whatever
Process proc = processBuilder.start();
See ProcessBuilder javadoc for more info.
Using process builder to launch other Java applications in their own OS process. The implementation works on Windows 7, but not on Linux. Both machines are using Java 1.7. Here is some example code:
//Windows OK, but Linux Could not find or load main class
//weka.subspaceClusterer.MySubspaceClusterEvaluation
ArrayList<String> commands = new ArrayList<String>();
commands.add("java");
commands.add("-cp");
commands.add("\".:lib/*\"");
commands.add("weka.subspaceClusterer.MySubspaceClusterEvaluation");
procBuilder = new ProcessBuilder();
procBuilder.inheritIO();
procBuilder.command(commands);
Process proc = procBuilder.start();
I encountered a similar issue on Mac OS X. It worked in Terminal but not in Eclipse. It worked for me if I removed quotes around the class path string. I guess Eclipse JVM does not like it when there are quotes around any of the arguments passed to ProcessBuilder.
The classpath separator is ; under Windows, but : under Unix.
Consider creating a runnable jar, where your classpath is stored in the MANIFEST.MF entry, so you can just execute java -jar whatever.jar.
Your code looks correct. It just can't find your class file. Try setting the working directory of your process:
procBuilder.directory(new File("package/structure/starts/here"));
I need to execute linux commands from JSP.
It is working fine.
But i need to start some sh file in a particular directory in linux through JSP. say /home/username/something/start.sh
try{
String command= "cd /home/username/something";
Runtime.getRuntime().exec(command);
Runtime.getRuntime().exec("./start.sh")
out.println("Child");
}
catch(Exception e)
{ out.println("Error");
}
It says FIle or Directory not found.
I tried Runtime.getRuntime().exec("pwd"), It is showing something like "java.lang.UNIXProcess#fc9d2b" !! :O
I need to change the pwd and execute some commands through jsp. How can i do that??
Any help would be appreciated.
You shouldn't (and actually, it seems you can't) set a working directory like that. Each Process object given by Runtime.exec() will have its own working directory.
As answered in How to use “cd” command using java runtime?, you should be using the three argument version of Runtime.exec(), in which you provide a File that will be the working directory. From its javadoc:
Executes the specified command and arguments in a separate process with the specified environment and working directory.
Or even better, use ProcessBuilder along with ProcessBuilder.directory() instead:
ProcessBuilder pb = new ProcessBuilder("start.sh");
pb.directory(new File("/home/username/something"));
Process p = pb.start();
I compile java file by Runtime.exec("javac MyFrog.java");
It says no errors, doesn't output anything, but doesn't create MyFrog.class file.
if i write Runtime.exec("javac") it outputs to output some help text.
So I understand that program is working, but don't create class file. Permissions are ok.
javac -verbose should give you lot more information, specifically the directory where the file is created. Since you are able to recognize output help text without any parameters, I assume you are capturing the process's stderr and assume you are doing the same for stdout as well (though javac does not seem to write anything to stdout).
Where are you checking for the existence of the .class file? It is created in the same directory as the .java file. If MyFrog.java has a package declaration it will not be created in the package sub-dir; for that you have to use -d argument.
make sure that MyFrog.java is present in working directory
Try javac -verbose and make sure your classpath is set correct.
You can use ProcessBuilder or Runtime.exec() method to create new process,
String []cmd={"javac","Foo.java"};
Process proc=Runtime.getRuntime().exec(cmd);
proc.waitFor();
Or use ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("javac.exe","-verbose", "Foo.java");
pb.redirectErrorStream(true);
Process p = pb.start();
p.waitFor();
InputStream inp=p.getInputStream();
int no=inp.read();
while(no!=-1)
{
System.out.print((char)no);
no=inp.read();
}
I always find this resource answers all questions about Java exec.