3

I am building a GUI in Java (Swing) and I have to execute Java code from that. For the sake of testing simple code like printing HelloWorld in Java will be ok.

I have seen the forum questions and I just know that I have to invoke Operating System (I am using Windows7) to execute that compilation task.

Thank you.

P.S: I have tried with Runtime.getRuntime().exec() command but no success.

2
  • 1
    Welcome to Stackoverflow! It is usually best to post some code to show what you have tried - e.g. How does your Runtime.getRuntime().exec() call look like? Also, it is not clear from your question whether you want to execute a separate process or whether you simply want to call a Java method - for that you do not need exec() Commented Oct 15, 2012 at 13:04
  • I doubt that this question is answerable, please to read FAQ Commented Oct 15, 2012 at 13:05

4 Answers 4

3

If you are using IDE you don't need to call these commands.

Compile:

  • javac HelloWorldSwing.java

Run:

  • java HelloWorldSwing

....

If you want to use Runtime.getRuntime().exec(). Here is an example of using Runtime.getRuntime().exec()..

import java.io.*;  
public class TestExec {  
    public static void main(String[] args) {  
        try {  
            Process p = Runtime.getRuntime().exec("cmd /C dir");  
            BufferedReader in = new BufferedReader(  
                                new InputStreamReader(p.getInputStream()));  
            String line = null;  
            while ((line = in.readLine()) != null) {  
                System.out.println(line);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

"This just runs the dir command, captures its ouput and copies it to the programs stdout. Not very exciting but it shows the basic parts to use Runtime.exec(). You can also open up the process' output and error streams. "

so you can send commands with Runtime.getRuntime().exec(), you can use javac or java commands that i have wrote above.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I created a process that invoked command prompt to compile java file (javac) and then ran it. Grabbed the output in the same manner you mentioned. Thank you again.
2

you might want to check out the java compiler api for the compiling code part, runtime.exec() should work with the correct commands to launch the app.

2 Comments

I am checking that. Keeping in mind that I'll be in GUI application, not in eclipse (compiler), while compiling and running java code.
@user1747048 eclipse is not the same as a compiler. The link already contains a code snippet showing how to compile a file
1

There is an example of using the JavaCompiler API in the SSCCE Text Based Compiler (STBC). Be sure to read the pages related to getting a tools.jar on the run-time class-path.

The STBC is open source.

Comments

1

For compiling you need an installed JDK which includes the Java compiler javac. You can call javac using via Runtime.getRuntime().exec() for compiling Java source code and then load it.

Alternatively you can use the Java Compiler API. The main compiler API can be retrieved via javax.tools.ToolProvider.getSystemJavaCompiler();

See also this article: The Java 6.0 Compiler API

2 Comments

What is the best way to do so? 1. call javac using via Runtime.getRuntime().exec()? OR 2. Java Compiler API?
The JavaCompiler API is superior. E.G. you get direct access to the compilation errors and can decide what to do with them (e.g. drop them into a JList, show them into a JTextArea..). See my answer for a link to a demo. & source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.