关于Eclipse中一个错误排查总结

今天在进行代码集成测试的时候,对集成测试的java代码进行 run-Junit Test,本来应该console应该打印出来运行信息的

 

111

 

 

但是实际console打印出来的如下图所示:

222

 

 

个人觉得相当好奇,但是在同一个workset的里面的其他工程就没有问题

 

根据eclipse的运行原理,其实run-junit Test 就是 执行javaw –classpath ***/**.jar ***/***/Test 类似的命令 和run-application 其实是一样的。那么就写了添加了一个main方法,期望应该是一样的结果,果真运行run-application之后,console显示的结果和上面的一样

 

开始觉得是eclipse的问题或者修改过代码的问题,重新下载了代码和新的eclipse结果都还是一样,后来又确认了jvm的版本,全部都是用jdk1.6.0_31的版本,以前的版本都删除了,但是还是一样的问题。

 

在推理,发现workset的其他工程里面的代码都是可以运行的,发现估计和eclipse和jvm没有太大的关系。联想到这个工程依赖的jar包很多,是不是和这个有关。先删除了部分依赖的jar包(删除了一半),在运行程序,发现没有报以上错误了。觉得问题可能出现在这里。 初步怀疑是不是classpath太长,导致javaw 命令无法执行。

我们所有的功能依赖jar都是通过mvn管理的,默认mvn仓库是c:\documents and settings\用户名\repository里面, 所以每一个依赖的jar包,都有这个前缀,我先减少这个前缀尝试一下,所以就把默认的repo目录指向了d:\repo目录,重新编译执行依赖,刷新eclipse代码,再次执行run-application,发现程序正常运行。后来为了验证我的猜测,写了一段代码进行验证:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Test {
    public static void main(String[] args) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("javaw ");
        sb.append("-classpath ");
        //构造长的路径classpath
        for (int i = 0; i < 1000; i++) {
            sb.append("D:/repo/log4j/log4j/1.2.13/log4j-1.2.13.jar,");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(" com.company.test.Test");
        System.out.println(sb.toString());
        //执行命令
        Process process = Runtime.getRuntime().exec(sb.toString());
        //打印结果
        List<String> list = IOUtils.readLines(process.getInputStream());
        for (String s : list) {
            System.out.println(s);
        }
    }
}

 

运行这段代码,应该打印出来的结果和上面一样,结果如下,符合我的猜测

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Usage: javaw [-options] class [args...]
           (to execute a class)
   or  javaw [-options] -jar jarfile [args...]
           (to execute a jar file)
 
where options include:
    -client   to select the "client" VM
    -server   to select the "server" VM
    -hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.
                   
    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose[:class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image

 

我们把命令打印出来,copy到cmd命令行里面执行,发现命令行不完整,被自动截取了

3333

 

这个命令根本都不完整,后面指令的class丢失了,javaw参数中没有指定要运行的类,就导致我们看到的结果。

这个可能是windows下,和shell指令的长度限制有关。

原文地址:https://www.cnblogs.com/daichangya/p/12959698.html