0

i need a java program in java that compiles other java programs using cmd commands

1
  • Maybe you should just use ant (see ant.apache.org) :-) Commented Nov 8, 2010 at 10:15

7 Answers 7

5
Runtime.exec( -whatever cmd command you need to execute- )

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html

Vinod.

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

3 Comments

For JDK 1.5 and up, you can use ProcessBuilder (download.oracle.com/javase/1.5.0/docs/api/java/lang/…)
I edited your answer since there is no RunTime in Java, it's lowercase T. Note: I don't think that exec can run a cmd command directly, you need to run cmd.exe...
oops thanks for pointing out the case mismatch Carlos.. bt i guess we shall be able tu run any cmd commands basically shell commands rite..
2

Maybe you are looking for Java Runtime.exec() function:

exec
public Process exec(String command)
              throws IOException

Executes the specified string command in a separate process. This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).

1 Comment

can u give me a sample program
1

To execute real cmd commands you need to start cmd.exe with the /c option using Runtime.exec or a ProcessBuilder like

    String cmd = "dir > t.txt";
    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", cmd);
    Process process = builder.start();
    process.waitFor();
    System.out.println("done");


To start an executable like calc.exe you can start it directly

    ProcessBuilder builder = new ProcessBuilder("calc.exe");
    Process process = builder.start();
    process.waitFor();
    System.out.println("done");

both code samples missing IO and Exception handling...

Comments

1

Additional note:

If using JDK1.6 you can now programmatically compile from another java program using JavaCompiler. You could invoke your compiler program from the command line if this is what you are trying to achieve.

Comments

0

using the cmd could be done like this:

String cmd = "c:\\Programme\\Ghostgum\\gsview\\gsprint.exe"; //what to execute
String prt = "-printer XYZ"; // additional parameters
String dat = "\"" + pfad + "\""; // the file to be passed
ProcessBuilder pb = new ProcessBuilder(cmd, prt, dat);
System.out.println(cmd + " " + prt + " " + dat);
pb.redirectErrorStream(true);
Process ps = pb.start();
ps.waitFor();

1 Comment

Note 1: not running cmd.exe, the shell variable is not being used at all. 2: gsprint.exe is not a cmd command. 3: there is no reason to call redirectErrorStream if you are not reading the standard output (not wrong anyway).
0

Not sure why you'd want to explicitly invoke the shell in order to compile Java programs. if you're absolutely sure that this is what you need to do, then go for it and follow the suggestions given by the others. However, if all you want to do is to compile Java code from within a Java program, you can do that with Java 6.0 (and up):

http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

Comments

0

I finally got my answer. It actually compiles a Java program. The program is as follows:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Dos {
    public static void main(String[] args) {
        try {
            String[] command = new String[4];
            command[0] = "cmd";
            command[1] = "/C";
            command[2] = "C:/Program Files/Java/jdk1.6.0_21/bin/javac";//path of the compiler
            command[3] = "d:\\a.java";

            Process p = Runtime.getRuntime().exec(command);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command

            String s = null;
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // 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);
            }
            System.out.println("I am In try");
        }

        catch (Exception e) {
            System.out.println("I am In catch");
        }
    }
}

1 Comment

As a compiler, I would refuse to compile this mess. Please use proper code formatting (preferably the Java Code Conventions). :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.