简体   繁体   中英

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.

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.

确保MyFrog.java存在于工作目录中

尝试javac -verbose并确保您的类路径设置正确。

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();
} 

我总是发现这个资源回答了有关Java exec的所有问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM