Java:从javap派生类路径?

时间:2015-11-13 07:06:30

标签: java maven

如何从运行javap派生类路径?

这是一个maven生成的项目,我似乎能够运行hello-world应用程序,但不能运行测试程序。

这有效:

~/junk11/hello $ javap target/classes/net/markharrison/App.class 
Compiled from "App.java"
public class net.markharrison.App {
  public net.markharrison.App();
  public static void main(java.lang.String[]);
}

~/junk11/hello $ java -cp target/classes net.markharrison.App
Hello World!

但不是AppTest类:

~/junk11/hello $ javap target/test-classes/net/markharrison/AppTest.class 
Compiled from "AppTest.java"
public class net.markharrison.AppTest extends junit.framework.TestCase {
  public net.markharrison.AppTest(java.lang.String);
  public static junit.framework.Test suite();
  public void testApp();
}
~/junk11/hello $ java -cp target/test-classes net.markharrison.AppTest
Error: Could not find or load main class net.markharrison.AppTest

测试从Maven开始:

~/junk11/hello $ mvn test -q
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running net.markharrison.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

我错过了哪一块拼图?

1 个答案:

答案 0 :(得分:0)

由于测试用例使用junit,AppTest没有主要的方法来调用。这可以通过命令行确定,如下所示:

$ javap AppTest.class |fgrep 'public static void main(java.lang.String[])'
$ if test $?; then echo can be run; fi