I'm developing java application which allow to white or paste a source-code and run it without doing compiling procedure manually.what it do is save java code from text-box to a text-file and execute cmd command java java-file after execute javac java-file.file.
it's work fine but this problem came across when java file doesn't have any output .i mean if code create a swing form ..cmd haven't any output.then my java program get stuck actually i can't close it.i have to use external program to close it like taskmanager.
but when source-code has command-line output this problem never occurred .
OutputStream stdin = null;
BufferedReader br = null;
String line = null;
String command = null;
command = jTextField1.getText();
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
stdin = p.getOutputStream(); //use this to push commands
//processing stdout
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
// br.flush();
byte[] bytes = new byte[4096];
///////////////////////////////////////////
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
while (in.read(bytes) != -1)
{
jTextArea1.append(br.readLine()+"\n");
}
///////////////////////////////////////////
Related
I have a Java script that starts a new thread to execute a python script using Process builder. The code below currently takes the output from python and displays it in the Java run output and within a JTextArea. BUT, it only does so in bulk, once the py script has finished running. Is there a way to get the output displayed live as it is written out from the py script? Thanks!!!!
public void launchPythonScript() {
try {
ProcessBuilder py = new ProcessBuilder("cmd", "/C", "PythonScriptLocation (C:\\....)",""+Directory(variable needed for py script));
Process launch = py.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(launch.getInputStream()));
String readLine;
StringBuilder JavaOutput = new StringBuilder();
while((readLine = reader.readLine()) != null){
JavaOutput.append(readLine).append(System.lineSeparator());
frame2.consoleOutput.setText(JavaOutput.toString());
System.out.println(readLine);
}
} catch (IOException ex) { Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, ex);}
}
Process InputStream should give you data while the script is running.
You are reading one line at time so if your python script send all data in one single line then you see the output only at the end.
Try to break the output data into multiple lines.
I'm trying to execute a script via JAVA code, but it's not being executed. I tried execute() of Process class but later switched to ProcessBuilder after some searching hoping to make this work. But the script's not getting executed.
JAVA Code:
String fileName = "pkgdiff.sh";
File file = new File(fileName);
ProcessBuilder builder = new ProcessBuilder("/bin/sh", fileName);
builder.directory(file.getParentFile());
Process process = builder.start();
process.waitFor();
StringBuffer output = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
LOGGER.info("### Script Execution result --> " + fileName+"-->" + output);
Script file:
#!/bin/sh
.. rest of the content
How much output is the script producing? You should be processing its output before you call waitFor(), otherwise the process might block if it fills up its output buffer.
From the Java API:
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.
I'm writing a Java program, that needs to run an exe file compiled by cygwin. After running it, it needs to enter two lines of queries (each ending by \n). and on the third line it will receive an answer. I need to read that answer to process in the rest of the program.
The second line that I'm inputting is the path to another file! If I run the same command from CMD or from cygwin and input that path it works, but when I'm entering the path (hardcoding it in my Java program), the invoked exe file returns that there is no such file. Is there any special path instruction in this case?
(I have the following code which runs the program and gets the program output, but before getting my desired result, the exe gives an error that there is no such file and exits!
ProcessBuilder pb = new ProcessBuilder("C:/Users/Armen/Downloads/oll-0.03/oll_line.exe","P");
pb.redirectErrorStream(true);
Process cmd = pb.start();
PrintWriter pWriter = new PrintWriter(cmd.getOutputStream());
pWriter.println("L C:/Users/Armen/workspace/recurdom/model0fsupernew");
pWriter.flush();
//HERE IT GIVES ME THE ERROR
//pWriter.close();
InputStream is = cmd.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
line = br.readLine();
System.out.println("here received: "+line);
//pWriter.println("T +1 5:1 51:1 70:1 92:1 121:1 142:1 181:1 202:1 215:1 250:1 272:1 291:1 330:1 347:1 371:1 409:1 423:1 449:1 481:1 513:1 521:1 522:-1 523:-1 524:-1 525:-1 526:-1 527:-1 528:-1 529:-1 530:-1 539:1");
//pWriter.flush();
I have the following code in java that calls the date command in the command prompt:
// prepare command prompt runtime and process
Runtime runtime = null;
Process process = null;
// prepare output stream
OutputStream outputStream = null;
try {
runtime = Runtime.getRuntime(); // instantiate runtime object
process = runtime.exec("date"); // get the current date in command prompt
// read the output of executing date command
outputStream = process.getOutputStream();
// output the date response
System.out.println(outputStream);
process.waitFor(); // wait for the date command to finish
} catch(Exception e) {
e.printStackTrace();
} // end catch
How can I read the outputStream value for me to be able to use the System.output.println()
You don't read the output stream, you write to it to pass data to process. To read the data from process use
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
br.readLine();
The code is for string output of process. Of course if your process outputs data in other way you have to change the wrappers around process.getInputStream()
Update: I think it is in some way confusing that we use getInputStream to actually read process output :) The reason is that initially basic classes OutputStream and InputStream were named so relatively to the code that uses them (the code you write). So when you use OutputStream you actually use it as output for your program. When you use process.getOutputStream you don't get process' output but instead get your program output which is piped to process input. When you use process.getInputStream you get input for your program which obtains data piped from process' output.
you can do like this way without using OutputStream object
Process p = Runtime.getRuntime().exec("date");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String answer = sb.toString();
System.out.println(answer);
Okay i'm trying to make ChucK available in exported Processing sketches, i.e. if i export an app from Processing, the ChucK VM binary will be executed from inside the app. So as a user of said app you don't need to worry about ChucK being in your path at all.
Right now i'm generating and executing a bash script file, but this way i don't get any console output from ChucK back into Processing:
#!/bin/bash
cd "[to where the Chuck executable is located]"
./chuck --kill
killall chuck # just to make sure
./chuck chuckScript1.ck cuckScriptn.ck
then
Process p = Runtime.getRuntime().exec("chmod 777 "+scriptPath);
p = Runtime.getRuntime().exec(scriptPath);
This works but i want to run ChucK directly from Processing instead, but can't get it to execute:
String chuckPath = "[folder in which the chuck executable is located]"
ProcessBuilder builder = new ProcessBuilder
(chuckPath+"/chuck", "test.ck");
final Process process = builder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while((line = br.readLine()) != null) println(line);
println("done chuckin'! exitValue: " + process.exitValue());
Sorry if this is newbie style :D
ProcessBuilder builder = new ProcessBuilder
(chuckPath+"/chuck", chuckPath+"/test.ck");
the args all need an absolute path.