Java -cp and -jar

Just a tip about java command.

It seems the -cp option is not compatible with the -jar option …

A colleague asked me how to launch a Main class within a jar AND having an other jar in the classpasth. According to java -help it seems possible to write something like:

java -cp lib.jar -jar app.jar

But it doesn’t work and results in NoClassDefFoundErrors … the way to do that we found is :

java -cp lib.jar;app.jar app.Main

May be we missed something here ….


7 thoughts on “Java -cp and -jar

  1. You should insert jar dependencies into manifest data. In your example the manifest file in app.jar should be like this:

    Manifest-Version: 1.0
    Main-Class: app.Main
    Class-Path: lib.jar

    then the application can be started like this:

    java -jar app.jar
    (without command line classpath definition)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s