Start and get output of a .jar (E.g. Minecraft Bukkit .jar) - java

I'm trying to start some .jar inside an .jar using this code: (I'm trying with craftbukkit server right know)
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java -jar craft.jar");
BufferedReader out = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter in = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
this.running = true;
while(this.running){
String line;
if((line = out.readLine()) != null){
System.out.println(line);
}
}
The problem is, it work for a moment, but after a while it stops outputting or just print '>' as show in the image below. How can I fix this? **Plus, Which is the correct way to send commands to the bukkit server?
Thanks!

When using a Process in Java, it is crucial to read stdout and stderr in two independend threads (your while loop). Otherwise the child process can lock up waiting for the filled buffer to be drained.
With Java 7 you can actually redirect stderr to stdout, and possibly redirect the resulting stream to the parents stdout. With older versions you need to attach the seperate threads.
Besides: you should not use the "single string" variant of exec, but specify the args as arrays. This is safer (less vulnerable to parsing problems).
(But I am not sure if this is your actual problem. Maybe you should dump the error stream and see if it helps)

I do it like that:
#Override
public void run(){
// ...
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); //of course proc is an instance of Process, and it's set up
while(true){
String line = stdInput.readLine();
if(line == null) break; //this is essential, when process terminates, line gets null
System.out.println(">"+line);
}
// ...
}
Give it a try.
(Technically it's similiar how you did, but it works for me so i share it)

Related

process.waitFor() returns one on of my computers but not on my other computer

I have this code
private static void restartTor() throws IOException, InterruptedException {
String killTor = "killall tor";
String startTor = "/opt/local/bin/tor -f /dev/torrc";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(killTor);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
pr = run.exec(startTor);
pr.waitFor();
buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
}
When I run this on computer A it executes as expected but when I run it on my second computer, B, it gets stuck at the second pr.waitFor();.
I have read a bunch of questions here on SE, such as process.waitFor() never returns and Java process.waitFor() does not return and the main issue there seems to be that you don't read the buffer but I do that (don't I?).
A and B are similar, but not identical (Macs, running 10.15, A has 32 GB RAM, B has 16 GB RAM).
I use the same version of tor and the torrc:s are identical on A and B.
I am stumped. What is the problem here?
Edit: On B, If I manually, from a regular terminal, kill the process, it returns and everything continues as expected.
Edit 2: Now it fails on computer A as well. I had run it dozens of times there, without problems before but now it fails constantly.
I don't know if this this is the ultimate cause of your problem, but you should call waitFor after reading the output (and errors) from the external process. If a process writes more to its standard output and error streams than the OS is prepared to buffer, then your code could deadlock:
The external process is blocked while trying to write output
Your Java code is blocked waiting for the external process to exit.
You need to consume both streams at same time if you are experiencing the stream deadlock issue on waitFor(). You can do this with background threads on pr.getErrorStream(), or set up STDERR handling before calling waitFor().
To do this replace use of Runtime.exec with ProcessBuilder.
ProcessBuilder pb = new ProcessBuilder(startTor);
This is easier with either sending error log to a file:
File stderr = new File("stderr.log");
pb.redirectError(stderr));
... or just redirect error to merge it with stdout:
pb.redirectErrorStream(true);
then
pr = pb.start();
pr.waitFor();

Determine if a java execution from another java application was done successfully

I have an application that calls a java test class in a specified location of my PC. The path is hard-coded for now, and I checked that it worked by executing it from the command line (in case you want to see it: java -cp C:\Users\user\Documents\workspace\test\build\test.jar org.junit.runner.JUnitCore us.test.DynamicWebserviceInvocationTest), so I know that the command works fine.
The thing is, when I do Runtime.getRuntime().exec(command), if I try to log the resulting InputStream and ErrorStream of its resulting process, the program stucks. I tried with exitValue() and waitFor(), but the first throws an incompletition error and the second also gets stuck. The weird thing is that if I don't touch anything of this (the streams, or using the functions), the program has no problem ending.
So my question is: Why could this be? The next step is to build the command with given parameters, but if I can't see the resulting inputs I can't be completely sure if the tests are running or not.
The code, in case you want to see it:
Runtime runtime=Runtime.getRuntime();
logger.debug("Attempting to execute the test {} at path {}",classpath,applicationLocation);
String command="java -cp C:\\Users\\user\\Documents\\workspace\\test\\build\\test.jar org.junit.runner.JUnitCore us.test.DynamicWebserviceInvocationTest";
Process process=runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
You absolutely must read both streams in separate threads. (Read the Javadoc of the Process class). If you wait for the end, or read first one stream before the other it can happen that the output buffer of the command fills up and it will block (on stdout or stderr, depending on what you read first). Normally you use waitFor() in the current thread and have background thread draining the outputs (this also allows detecting end of child process without polling).
If you want to use only one (additional) thread you can redirect stderr to stdout. If you want to avoid to read the streams at all, you can set the
ProcessBuilder features inheritIO(). This allows the stream to be written to the existing output and so you do not need a thread to read independently.
There are BTW various libararies which offer exec tools (for example Apache Commons Exec has some streams) which offer active stream draining, logging or pumping for Process.
In addition to that it might also be a good idea to first close stdin, in case the command waits for input: p.getOutputStream().close();
Make sure you close the 2 streams:
String command="java -cp C:\\Users\\user\\Documents\\workspace\\test\\build\\test.jar org.junit.runner.JUnitCore us.test.DynamicWebserviceInvocationTest";
Process process=runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
stdInput.close();
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
stdError.close();

Java run Process unable to get standard out

I have my code below. I am trying to run my ruby script and get the standard out, I don't really need standard error in this case. For some reason, I could not get any output at all in my production host, and I only get empty string. The same code works fine in my desktop. If I run my ruby script myself, I could get output from it. My ruby script only has a puts statement for now. Any one know the reason? Is it that I need to some kind of block to wait for the ruby script to finish?
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("ruby", "tests.rb");
Process process = processBuilder.start();
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = bufferReader.readLine()) != null) {
System.out.println("Line is: " + line);
output.append(line + "\n");
}
bufferReader.close();
return output.toString();
I have redhat machine running zsh.
You need to close the process's output stream (its standard input), and you should probably merge the output and error streams. You might think you don't want them both but you might be wrong, and if the process produces enough unread error output it will stall.

Running other programs from Java

I need to run a couple of other programs from my own Java program, basically I need to run these command line statements.
svn log --xml -v > svn.log
and
java -jar example.jar arg1 arg2
and I need to use the text outputs written to the console from these programs in my own program. I've tried Runtime.getRuntime().exec() with the svn, but it doesn't seem to be doing anything because it doesn't make a svn.log file. Also both programs need to be called in different places, the svn line needs to be called from inside one folder and the java line needs to be called from another.
Any ideas on how to go about this? If this is not possible in Java, is there a way to do it in C#?
Thanks
Here:
ProcessBuilder processbuilder
try
{
processbuilder.directory(file);
processbuilder.redirectErrorStream(true);
process = processbuilder.start();
String readLine;
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
// include this too:
// BufferedReader output = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((readLine = output.readLine()) != null)
{
m_Logger.info(readLine);
}
process.waitFor();
}
I've used something similar. You'll actually want to do something with the readLine. I just copied and pasted from code where I didn't care what it said.
The redirection > (like the pipe |) is a shell construct and only works when you execute stuff via /bin/sh (or equivalent). So the above isn't really going to work. You could execute
/bin/sh -c "svn log --xml -v > svn.log"
and read svn.log.
Alternatively, you can read the output from the process execution and dump that to a file (if you need to dump it to a file, or just consume it directly as you read it). If you choose this route and consume stdout/stderr separately, note that when you consume the output (stdout), you need to consume stderr as well, and concurrently, otherwise buffers will block (and your spawned process) waiting for your process to consume this. See this answer for more details.
instead of piping in your command, just let it print to standard output and error output. You can access those streams from your process object that is returned from exec.
For the svn stuff use java SVNKit API.
Seeing your two commands, why don't you do it directly from Java, without executing ? You could use SVNKit for the svn part, and include directly the jars in your classpath.
Try this
public static void main(String[] args) {
try {
// Execute a command with an argument that contains a space
System.out.println(args[0]);
String[]commands = new String[]{"svn", "info", args[0]};
Process process = Runtime.getRuntime().exec(commands);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.println(result);
}catch(Exception e){
System.out.print(e);
}
}

External program from our Java program

How can I write a program in Java that will execute another program? Also, the input of that program should be given from our program and the output of that program should be written into a file.
This is my small set of code to get its output:
Process p = Runtime.getRuntime().exec("C:\\j2sdk1.4.0\bin\\helloworld.java");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
This was my set of code but this throws an IOException.
The API that Java offers for this is the ProcessBuilder. It is relatively straightforward to set working directory and pass parameters.
What is a little tricky is passing STDIN and reading STDERR and STDOUT, at least for non-trivial sizes thereof, because you need to start seperate threads to make sure the respective buffers get cleared. Otherwise the application that you called might block until it can write more output, and if you also wait for that process to finish (without making sure that STDOUT gets read), you will deadlock.
You can use java.lang.Process and java.lang.ProcessBuilder. You interact with the input/output of the process using getInputStream/getOutputStream/getErrorStream.
However, there's an Apache Commons library called Exec which is designed to make all of this easier. (It can normally get quite hairy when it comes to quoting command line parameters etc.) I haven't used Exec myself, but it's worth checking out.
When you only want to start other programms, you can use the exec method like this:
Runtime r = Runtime.getRuntime();
mStartProcess = r.exec(applicationName, null, fileToExecute);
StreamLogger outputGobbler = new StreamLogger(mStartProcess.getInputStream());
outputGobbler.start();
int returnCode = mStartProcess.waitFor();
class StreamLogger extends Thread{
private InputStream mInputStream;
public StreamLogger(InputStream is) {
this.mInputStream = is;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(mInputStream);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
exec:
public Process exec(String command, String envp[], File dir)
#param command a specified system command.
#param envp array of strings, each element of which
has environment variable settings in format
<i>name</i>=<i>value</i>.
#param dir the working directory of the subprocess, or
<tt>null</tt> if the subprocess should inherit
the working directory of the current process.
Please do not edit your question so that it does not fit the original answers anymore.
If you have follow-up question, clearly mark them as such, or ask them as a seperate questions, or use comments or something.
As for your IOException, please give the error message it shows.
Also, it seems as if you are trying to run a ".java" file directly. That will not work. The methods described here are to launch native binary executables. If you want to run a ".java" file, you have to compile it to a class, and the invoke that class' main method.
What platform are you in?
If you are on *nix you can type:
java MyProgram | myexternalprogram > myfilename.txt

Categories