简体   繁体   中英

scala jar file executed by java - cp vs. jar

if I run:

java -jar corpus-tools-0.0.2.jar removeTSfromCorpus

it gives me error:

Failed to parse the trailing argument list: 'removeTSfromCorpus'

However, if I run:

java -cp corpus-tools-0.0.2.jar removeTSfromCorpus

It works seamlessly. scala-library is inlcuded in dependencies(MANIFEST). What is difference in -cp and -jar? I think there should be equal in this case

thanks!

java -cp jarfile classname

executes classname using the specified classpath ( -cp ). Instead of using the -cp option you could simply rely on the CLASSPATH variable to determine where java finds the classes.

java -jar jarfile

will use the specified .jar file and execute the Main-Class defined in the .jar file MANIFEST. This is java's close approximation to a standalone app. The app is packaged in the .jar file and the MANIFEST specifies the entry point within that .jar file. See here for more details.

So (to answer your original question!) your first example will run a class specified in the MANIFEST, and that's trying to interpret removeTSFromCorpus as a command line argument in some fashion. Your second example sets the CLASSPATH to your .jar file and then runs removeTSFromCorpus as a class.

When running a JAR the main class and classpath should be specified in the MANIFEST.MF file.

You then just run it like:

java -jar corpus-tools-0.0.2.jar

See:

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

Extract

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

And

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

Extract:

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

The -jar option is trying to execute the static main method from the main class defined in your jar file, then provides it with the argument removeTSfromCorpus .

The -cp option considers you are providing a classpath then trying to run the main method from the removeTSFromCorpus class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM