简体   繁体   English

Runtime.exec不编译java文件

[英]Runtime.exec doesn't compile java file

I compile java file by Runtime.exec("javac MyFrog.java"); 我通过Runtime.exec编译java文件(“javac MyFrog.java”); It says no errors, doesn't output anything, but doesn't create MyFrog.class file. 它表示没有错误,不输出任何内容,但不会创建MyFrog.class文件。 if i write Runtime.exec("javac") it outputs to output some help text. 如果我写Runtime.exec(“javac”),它输出输出一些帮助文本。 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. javac -verbose应该为您提供更多信息,特别是创建文件的目录。 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). 由于您能够识别没有任何参数的输出帮助文本,我假设您正在捕获进程的stderr并假设您也对stdout执行相同的操作(尽管javac似乎没有向stdout写入任何内容)。

Where are you checking for the existence of the .class file? 你在哪里检查.class文件的存在? It is created in the same directory as the .java file. 它与.java文件在同一目录中创建。 If MyFrog.java has a package declaration it will not be created in the package sub-dir; 如果MyFrog.java有一个包声明,它将不会在包子目录中创建; for that you have to use -d argument. 因为你必须使用-d参数。

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

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

You can use ProcessBuilder or Runtime.exec() method to create new process, 您可以使用ProcessBuilder或Runtime.exec()方法创建新进程,

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

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

Or use ProcessBuilder 或者使用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的所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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