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.
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 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");
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.
I'm trying to develop a website that takes user input and converts to a text file. The text file is then used as an input for a .jar file. (e.g. java -jar encoder.jar -i text.txt), the jar then outputs a .bin file for the user to download.
This jar is designed to be run from command line and I really don't know the best way to implement it within a .jsp page. I have created a few java test classes but nothing has worked so far.
Does anyone have any suggestions on possible methods?
An alternative to running it as an external process is to invoke its main class in the current JVM:
Extract/open META-INF/MANIFEST.MF of the jar
Identify the Main-Class:. Say it is called EncoderMainClass
Invoke its main method: EncoderMainClass.main("-i", "text.txt")
This aught to be faster because a new OS process does not need to be created, but there may be security considerations.
Have you tried somrthing like this,
Create a java file
use a ProcessBuilder and start a new JVM.
Here is something to get you started:
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar", "thetextfile.txt");
pb.directory(new File("preferred/working/directory"));
Process p = pb.start();
ps: do handle to destroy process else it will eat up all memory
You can put this jar to the web application on the classpath and use it's class and methods. Better if you have javadoc if you don't have sources. But even if not in classpath you can try the example or this example.
I try to run batch file in java, but file performed not completely. Code look like:
Runtime.getRuntime().exec("cmd /c call "+path);
When I try to run it manually, without java, batch works correct.
Batch file contains next code:
cd C:\Downloads\
if not exist Documents mkdir Documents
move *.odt Documents
move *.doc Documents
if not exist Archives mkdir Archives
move *.tar Archives
move *.gz Archives
if not exist Music mkdir Music
move *.mp3 Music
Java complete batch only to fifth line. Has anyone faced this?
Hm, with batch, it stoped again here. And Java code: link. If add to somename.ogg to downloads folder, script goes to next line. All folders (Archives, Documents etc) are already exists, and downloads folder doesn't contain *.zip, *.tar, *.docx, *pdf files, but these lines passed without problems. Why it stoped precisely at ogg-line?
I think the problem is that there is a bug in your batch script. The MSDN documentation for the if command states this:
You cannot use the if command to test directly for a directory, but the null (NUL) device does exist in every directory. As a result, you can test for the null device to determine whether a directory exists. The following example tests for the existence of a directory:
if exist c:\mydir\nul goto process
Now the way you are using it (if not exist directory mkdir directory) is likely to succeed if the directory doesn't exist ... but attempt to create the directory a second time if it does exist. Ooops ...
Now, your Java application read and printed out the contents of the error stream for the process, you'd most likely see an error message telling you that the Archives directory already existed. Ignoring the output is asking for trouble, IMO.
I bet path in your Java program contains spaces or something similar. Are you quoting that properly?
Btw: Runtime.exec() has been superseeded with ProcessBuilder which can handle arguments much better than Runtime.exec()
Try this:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call \"" + path "\"");
builder.start();
OMG, GUYS!
I don't understand why, but:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\"");
builder.start();
doesn't works, BUT:
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "call", "\"" + "sort.bat" + "\"");
BufferedReader br = new BufferedReader(new InputStreamReader(builder.start().getInputStream()));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
works correctly! %)
If anybody understands this, please, explain this.
If you're not careful with your stdout and stderr streams it's very easy to deadlock ProcessBuilder or Runtime.exec(). The article When Runtime.exec() won't over at JavaWorld contains a lot of useful information on this and a couple of other pitfalls.
If possible, I would suggest using Commons Exec because all the required stream pumping is done for you.