關於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,發現程序正常運行。后來為了驗證我的猜測,寫了一段代碼進行驗證:

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

 

運行這段代碼,應該打印出來的結果和上面一樣,結果如下,符合我的猜測

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指令的長度限制有關。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM