2013-03-06 31 views
2

如果我運行:斯卡拉jar文件 - CP與罐子

java -jar corpus-tools-0.0.2.jar removeTSfromCorpus 

它給我的錯誤:

Failed to parse the trailing argument list: 'removeTSfromCorpus' 

但是,如果我運行:

java -cp corpus-tools-0.0.2.jar removeTSfromCorpus 

它可以無縫工作。 scala-library包含在依賴項(MANIFEST)中。 -cp和-jar有什麼區別?我認爲在這種情況下應該是相等的

謝謝!

回答

5
java -cp jarfile classname 

使用指定的類路徑(-cp)執行類名。而不是使用-cp選項,您可以簡單地依靠CLASSPATH變量來確定java找到哪些類。

java -jar jarfile 

將使用指定.jar文件並執行在.jar文件清單中定義的Main-Class。這是Java對獨立應用的近似值。該應用程序打包在.jar文件中,並且MANIFEST指定該文件中的入口點。有關更多詳細信息,請參閱here

所以(回答您的原始問題!)您的第一個示例將運行MANIFEST中指定的類,並且試圖以某種方式將removeTSFromCorpus解釋爲命令行參數。第二個示例將CLASSPATH設置爲.jar文件,然後將removeTSFromCorpus作爲類運行。

2

運行JAR時,應在MANIFEST.MF文件中指定主類和類路徑。

然後你只要運行它想:

java -jar corpus-tools-0.0.2.jar 

參見:

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

提取

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

Main-Class: classname

而且

http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

提取物:

You specify classes to include in the Class-Path header field in the manifest file of an applet or application. The Class-Path header takes the following form:

Class-Path: jar1-name jar2-name directory-name/jar3-name

1

-jar選項試圖從jar文件中定義的主類中執行static main方法,然後爲它提供參數removeTSfromCorpus

-cp選項認爲您正在提供類路徑,然後嘗試運行removeTSFromCorpus類中的main方法。