I am creating a code editor for Java source files by using Java. When I click on the RUN menu, it perfectly opens cmd and run javac command, but the problem is that it immediately gets closed and I want something like pause.
This is my code:
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd.exe /c start java Maq");
} catch (IOException e1) {
e1.printStackTrace();
}
How to get rid of this problem?
Now that you have confirmed that you actually need javac and are working on an IDE/editor, the right way to handle compiling errors is by compiling from the JRE using javax.tools.JavaCompiler.
The JavaCompiler gives you refined control, cross platform functionality and is always there. You can take a look at the JavaDoc to get yourself started. Here's a nice example of its usage.
I have used it personally for an IDE like project and it did exactly what you would expect from an in-program compiler.
The 'start' bit of your command detaches Java from the cmd process.
You're probably best off writing a batch script containing something like this:
#echo off
java Maq
pause
Then just call that batch file using Runtime.exec(...)
Answer from this question helped me :)
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd.exe /c cd /d d: & start cmd.exe /k javac Maq.java");
} catch (IOException e1) {
e1.printStackTrace();
}
To see javac's output, you can't use Runtime.exec(). Use a ProcessBuilder instead. Your code can look something like this:
ProcessBuilder pb = new ProcessBuilder("javac", "Maq.java");
pb.directory(new File("/path/to/source/code/"));
Process p = pb.start();
InputStream in = p.getInputStream(); // this is connected to the System.out from javac
int exit = p.waitFor(); // wait for javac to finish
StringBuilder text = new StringBuilder();
char[] buf = new char[1024]; int read;
while ((read = in.read(buf)) != -1)
text.append(new String(buf, 0, read));
text.append("\n\njavac returned with exit code ").append(exit);
// display text - it now contains javac's output and its exit code
JFrame f = new JFrame("javac's output");
JTextPane tp = new JTextPane();
tp.setText(text.toString());
tp.setEditable(false);
f.setContentPane(tp);
f.setSize(500, 400); // or whatever
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
Related
Am trying to get a series of commands on git bash one after the other. I can open the terminal through the code but after that wasn't successful with entering anything. For instance this is the code I tried
String [] args = new String[] {"C:\\Program Files\\Git\\git-bash.exe"};
String something="hi how are you doing";
try {
ProcessBuilder p = new ProcessBuilder();
var proc = p.command(args).start();
var w = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
w.write(something);
} catch (IOException ioException){
System.out.println(ioException);
}
Please let know how to be able to do enter a series of commands into git bash through the code.
The problem is that the command git-bash.exe opens the terminal window but the window's input is still the keyboard, so trying to write to the OutputStream that is returned by method getOutputStream(), in class Process does nothing. Refer to this question.
As an alternative, I suggest using using ProcessBuilder to execute a series of individual git commands. When you do that, your java code gets the command output.
Here is a simple example that displays the git version.
import java.io.IOException;
public class ProcBldT4 {
public static void main(String[] args) {
// C:\Program Files\Git\git-bash.exe
// C:\Program Files\Git\cmd\git.exe
ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Git\\cmd\\git.exe", "--version");
pb.inheritIO();
try {
Process proc = pb.start();
int exitStatus = proc.waitFor();
System.out.println(exitStatus);
}
catch (IOException | InterruptedException x) {
x.printStackTrace();
}
}
}
When you run the above code, the git version details will be written to System.out.
Also, if the git command fails, the error details are written to System.err.
You need to repeat the code above for each, individual git command that you need to issue.
Heere is the code I have so far. How do I have miktex-pdftex run?
List<String> processes = new ArrayList<String>();
processes.add("miktex-pdftex --output-directory=[Directory] [file_name].tex");
ProcessBuilder processbuild = new ProcessBuilder(processes);
First, you need to make sure the command you are using actually works at the command. If it does not, then it's not going to work in Java.
Next, one of the main reasons for using ProcessBuilder is to deals with spaces in the command/parameters better then Runtime#exec.
String command = "/Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex";
String outputDir = System.getProperty("user.dir");
String sourceFile = "Sample.tex";
List<String> commands = new ArrayList<>();
commands.add(command);
commands.add("--interaction=nonstopmode");
commands.add("--output-directory=" + outputDir);
commands.add(sourceFile);
So the above is very simple...
The command I want to run is /Applications/MiKTeX Console.app/Contents/bin/miktex-pdftex (I'm running on MacOS and I couldn't get the command installed outside the application bundle)
I want the output-directory to be the same as the current working directory (System.getProperty("user.dir")), but you could supply what every you need
I'm running in "nonstopmode" (--interaction=nonstopmode) because otherwise I would be required to provide input, which is just more complex
And my input file (Sample.tex) which is also in the working directory.
Next, we build the ProcessBuilder and redirect the error stream into the InputStream, this just reduces the next to read these two streams separately...
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
Next, we run the command, read the contents of the InputStream (otherwise you can stall the process), you can do what ever you want with this, I've just echoed it to the screen
try {
Process process = pb.start();
InputStream is = process.getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
int exitValue = process.waitFor();
System.out.println("");
System.out.println("Did exit with " + exitValue);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
The use int exitValue = process.waitFor(); here is just to ensure that command has completed and get the exit value it generated. Normally, 0 is success, but you'd need to read the documentation of the command to be sure
I need to run the below comand using java but it is running fine in terminal as
svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k > output.txt
But when running the same via process builder it is returning empty result.
My code:
ProcessBuilder pb = new ProcessBuilder("cmd", "C:\\Users\\dev112\\output", "svn", "list", "http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs", "--username", "prasadh", "--password", "prasadh2k", ">", "output.txt");
pb.redirectErrorStream(true);
try {
Process p = pb.start();
new Thread(new InputConsumerforImageMagick.InputConsumer(p.getInputStream())).start();
try {
System.err.println("Exited with: " + p.getErrorStream());
} catch (Exception ex) {
Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
}
I/O redirection doesn't work well with ProcessBuilder. You should either call cmd.exe with
new ProcessBuilder("cmd", "/c", "svn ... > output.txt");
(i.e. you have to call cmd with exactly two arguments)
or you must redirect yourself, that is you need to start a background thread which reads stdout from the process and writes it to output.txt. In that case, you should use:
new ProcessBuilder("svn", "list", ...);
The former is brittle when you have spaces in arguments. So I suggest the latter even though the Java code is much more complex.
You should also have a look at Commons Exec which makes it much easier to deal with external processes.
Or with Java 7, you can use pb.redirectOutput();
Don't go through cmd. Just run the command directly:
final Path cwd = Paths.get("c:\\Users\\dev112\\output");
Files.createDirectories(cwd);
final Path outfile = cwd.resolve("output.txt");
final ProcessBuilder pb = new ProcessBuilder("svn", "list",
"http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs",
"--username", "prasadh", "--password", "prasadh2k");
pb.directory(cwd.toFile());
pb.redirectOutput(outfile.toFile());
final int retcode = pb.start().waitFor();
What is more, why do you get the process' standard output if you output to a file? Do one or the other, not both. If you output to a file then read the contents of that file after the command is executed.
The sample above outputs to a file; just open a stream to that file afterwards using Files.newInputStream(outfile) (well, that is, if retcode is 0; if it isn't, your command has ended with an error; which also means that you should redirect stderr somewhere, too)
This works for me:
String command = "svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k";
ProcessBuilder processBuilder = new ProcessBuilder(command.split());
processBuilder.redirectOutput(new File("C:/Users/dev112/output/", "output.txt"));
processBuilder.start();
I just wanted to run a batch file using java code in win7. I can run .exe files with the code but u know it doesn't work with a batch. Where is the problem? You know even cmd.exe doesn't start with that command. But I can run other exe files, I've tried some. The code is this (with try and catch is that): none of them worked!
Runtime.getRuntime().exec("cmd.exe /c demo.bat");
Runtime.getRuntime().exec("demo.bat");
i tried to do work with process and i wrote the code below. it retuened
java.lang.IllegalThreadStateException:process has not exited
at java.lang.ProcessImpl.exitValue(Native Method)
at Test.Asli.main(Asli.java:38)
this is the code:
try{
Runtime rt = Runtime.getRuntime();
Process proc= rt.exec("C:\\Windows\\System32\\cmd.exe");
int b = proc.exitValue();
// int exitVal = proc.exitValue();
//System.out.println("Process exitValue: " + exitVal);}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Try the following:
String[] cmd = {"cmd.exe", "/c", "demo.bat");
Runtime.getRuntime().exec(cmd);
I always prefer splitting the command and the parameters myself. Otherwise it is done by splitting on space which might not be what you want.
Try this:
Runtime.getRuntime().exec("cmd.exe /c start demo.bat");
Use this:
try {
Process p = Runtime.getRuntime().exec("C:PATH/TO/FILE/yourbatchfile.bat");
} catch(Exception e) {
e.printStackTrace();
}
It even hides the annoying prompt window (if you want that)
I have written a java code in Eclipse and i am developing a plug-in which embed a button on workbench. When this button is clicked, it opens a batch file (located in c:/program file/prism 4.0/bin)
The code successfully opens the .bat file ! But my next task is write the output of that batch file on my console. I am using Eclipse IDE version 3.
My code is
MessageConsoleStream out = myConsole.newMessageStream();
out.println("We are on console ! \n Shubham performed action");
try {
ProcessBuilder pb=new ProcessBuilder("C:\\Program Files\\prism-4.0\\bin\\prism.bat");
pb.directory(new File("C:\\Program Files\\prism-4.0\\bin"));
Process p=pb.start();
int exitVal=p.waitFor();
out.println("Exited with error code "+exitVal+" shown and action performed \n");
out.println("Shubham Process Successful");
out.println("Printing on console");
}
catch (Exception e)
{
out.println(e.toString());
e.printStackTrace();
}
}
Do like this:
.....
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String in;
while((in = input.readLine()) != null) {
out.println(in);
}
int exitVal = p.waitFor();
.....
Note that if the batch file writes to standard error your java program must consume it otherwise the p.waitFor() will never return.
Do yourself a big favor and check http://commons.apache.org/exec/. It will take care of all the awful details about managnig an external process: timeout, handling input/output, even creating the command line will be easier and less error prone
Note that to correctly read from the InputStreams of a Process, you should do so on separate Threads. See this similar question.