ardakshalkar
ardakshalkar

Reputation: 625

Runtime.exec doesn't compile java file

I compile java file by Runtime.exec("javac MyFrog.java"); It says no errors, doesn't output anything, but doesn't create MyFrog.class file. if i write Runtime.exec("javac") it outputs to output some help text. So I understand that program is working, but don't create class file. Permissions are ok.

Upvotes: 0

Views: 1581

Answers (4)

KV Prajapati
KV Prajapati

Reputation: 94635

You can use ProcessBuilder or Runtime.exec() method to create new process,

String []cmd={"javac","Foo.java"};

Process proc=Runtime.getRuntime().exec(cmd);
proc.waitFor();

Or use ProcessBuilder

ProcessBuilder pb =  new ProcessBuilder("javac.exe","-verbose", "Foo.java");

pb.redirectErrorStream(true);
Process p = pb.start();
p.waitFor();

InputStream inp=p.getInputStream();
int no=inp.read();
while(no!=-1)
{
 System.out.print((char)no);
 no=inp.read();
} 

Upvotes: 0

Miserable Variable
Miserable Variable

Reputation: 28752

javac -verbose should give you lot more information, specifically the directory where the file is created. Since you are able to recognize output help text without any parameters, I assume you are capturing the process's stderr and assume you are doing the same for stdout as well (though javac does not seem to write anything to stdout).

Where are you checking for the existence of the .class file? It is created in the same directory as the .java file. If MyFrog.java has a package declaration it will not be created in the package sub-dir; for that you have to use -d argument.

Upvotes: 2

flash
flash

Reputation: 6810

Try javac -verbose and make sure your classpath is set correct.

Upvotes: 0

Nirmal- thInk beYond
Nirmal- thInk beYond

Reputation: 12054

make sure that MyFrog.java is present in working directory

Upvotes: 0

Related Questions