2013-03-06 63 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方法。