2

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?

msrd0
  • 7,816
  • 9
  • 47
  • 82
maq
  • 1,175
  • 3
  • 17
  • 34

4 Answers4

2

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.

Community
  • 1
  • 1
Mitchell
  • 29
  • 1
0

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(...)

Mitchell
  • 29
  • 1
  • but my code editor may have multiple tabs so shld i every time creates a batch file for evry tab to run them out?? – maq Oct 05 '14 at 12:14
  • 1
    Java is OS-independent by default, and this is very bad practice as well (no arguments ...) – msrd0 Oct 05 '14 at 12:41
  • maq, you could use arguments in the batch script. But msrd0 is right, and I completely agree with him, albeit no Runtime.exec call is cross platform when things like cmd.exe are involved. The proper solution is to use Java's Process functionality or as I posted as a new answer, the built in compiler – Mitchell Oct 05 '14 at 20:34
0

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);
msrd0
  • 7,816
  • 9
  • 47
  • 82
0

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();
                }
Community
  • 1
  • 1
maq
  • 1,175
  • 3
  • 17
  • 34