简体   繁体   English

在运行时执行java命令

[英]Execution of java command in Runtime

Process process2 = Runtime.getRuntime().exec(new String[]{"javac","-g:vars","/Users/amol/Documents/Java/a.java"});
Process process3 = Runtime.getRuntime().exec(new String[]{"javap","-l","-c","/Users/amol/Documents/Java/a"});

I tried to run this code but I am facing a strange problem. 我试图运行此代码,但我面临一个奇怪的问题。 It compiles correctly (means the first line compiles the program) but the second line gives an error saying that 'a' not found . 它正确编译(意味着第一行编译程序),但第二行给出错误,说'a' not found However when I checked the given directory a.class file was created correctly. 但是,当我检查给定目录时,正确创建了a.class文件。 How should I correctly run the second line? 我应该如何正确运行第二行?

javap takes a class name, not a filename. javap采用类名,而不是文件名。 You probably want to execute: 您可能想要执行:

javap -l -c -classpath /Users/amol/Documents/Java a

(Split that into string arguments appropriately, of course.) (当然,将它分成适当的字符串参数。)

Note that this will still fail if a is in a package - or if the class in a.java isn't actually a at all (which is valid for non-public classes). 需要注意的是,如果这仍然会失败a在包中-或者,如果在类a.java实际上不是a在所有(这是适用于非公共类)。 In both of these cases, you'd need to determine the classes involved, probably by building into an empty directory and finding out which files are produced by javac . 在这两种情况下,您都需要确定所涉及的类,可能是通过构建一个空目录并找出javac生成的文件。

您可能必须为javap指定类路径参数,直到该类的目录。

Process process3 = Runtime.getRuntime().exec(new String[]{"javap","-l","-c","-classpath  \"/Users/amol/Documents/Java/\"","a"}); 

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

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