67

I have a Maven project which generates a jar file and copies all dependencies to target/lib folder. I want to execute this project on client's machine (windows). So, I copied myproject.jar to C:\xyz folder and all dependencies to C:\xyz\lib folder. How do I execute this project from client's command prompt? I tried to use java -cp lib\*.jar -jar myproject.jar from C:\xyz folder but it throws following error.

Exception in thread "main" java.lang.NoClassDefFoundError: lib\commons-codec-1/3/jar
Caused by: java.lang.ClassNotFoundException: lib\commons-codec-1.3.jar
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: lib\commons-codec-1.3.jar.  Program will exit.

I think if I specify all dependencies in classpath (like java -cp lib\dep1.jar;dep2.jar), it will get rid of the problem but I don't want to do this as I have 40 libraries already and it might grow in future releases. Is there a better way to do this?

9 Answers 9

81

You cannot use both -jar and -cp on the command line - see the java documentation that says that if you use -jar:

the JAR file is the source of all user classes, and other user class path settings are ignored.

You could do something like this:

java -cp lib\*.jar;. myproject.MainClass

Notice the ;. in the -cp argument, to work around a Java command-line bug. Also, please note that this is the Windows version of the command. The path separator on Unix is :.

5
  • 2
    from what I read in the documentation, the * represents one or more jar files. So you need to say -cp lib*; You should not use it in the normal sense (one or more characters)
    – Brian
    Jul 30, 2014 at 14:26
  • 1
    Indeed, the *.jar didn't work for me (running on raspberry pi)
    – bvdb
    May 16, 2016 at 16:02
  • when tools that are supposed to make life easier for you actually cause more troubles...
    – Ahmadov
    Jul 22, 2016 at 10:48
  • I just want to add that this does not work currently PowerShell, but works find in CommandPrompt...
    – Amnestic
    Jun 12, 2017 at 14:06
  • 2
    It's a real shame both -cp and -jar are not available together. I have a CLI program that requires the user to supply a JDBC library to use it properly, and packaging it as an executable JAR file presents a challenge, here. Jun 10, 2018 at 18:16
68

Using java 1.7, on UNIX -

java -cp myjar.jar:lib/*:. mypackage.MyClass

On Windows you need to use ';' instead of ':' -

java -cp myjar.jar;lib/*;. mypackage.MyClass
2
  • pfew, I've been looking for this for 30 minutes now, trying to run an application on raspberry pi. Turns out I just needed to replace the ; separator with :. Thanks :)
    – bvdb
    May 16, 2016 at 15:44
  • Thank you. This worked for me. The windows version.
    – Coder17
    Nov 19, 2019 at 9:07
15

Let maven generate a batch file to start your application. This is the simplest way to this.

You can use the appassembler-maven-plugin for such purposes.

1
  • When you use -jar, -cp is ignored. On the other hand, '-cp lib/*.jar' is the same as '-cp lib/dep1.jar;lib/dep2.jar...', I think from java1.5 Dec 18, 2013 at 19:01
13

Regardless of the OS the below command should work:

java -cp "MyJar.jar;lib/*" com.mainClass

Always use quotes and please take attention that lib/*.jar will not work.

1
  • Actually the command differs according to the OS, you should use semicolon in windows and colon in unix Sep 22, 2020 at 16:36
3

You can use maven-assembly-plugin, Here is the example from the official site: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>your main class</mainClass>
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
2

a possible solution could be

create a batch file

there do a loop on lib directory for all files inside it and set each file unside lib on classpath

then after that run the jar

source for loop in batch file for info on loops

1

There are several options.

The easiest is likely the exec plugin.

You can also generate a jar containing all the dependencies using the assembly plugin.

Lastly, you can generate a file with the classpath in it using the dependency:classpath goal.

2
  • Our client doesn't have maven installed on their machine. Sorry, I should have mentioned this.
    – Praneeth
    Oct 22, 2012 at 19:42
  • I can use assembly plugin but not exec plugin
    – Praneeth
    Oct 22, 2012 at 19:44
1

I was running into the same issue but was able to package all dependencies into my jar file using the Maven Shade Plugin

-2

This will not work java -cp lib\*.jar -jar myproject.jar. You have to put it jar by jar.

So in case of commons-codec-1.3.jar.

java -cp lib/commons-codec-1.3.jar;lib/next_jar.jar and so on.

The other solution might be putting all your jars to ext directory of your JRE. This is ok if you are using a standalone JRE. If you are using the same JRE for running more than one application I do not recommend doing it.

7
  • 2
    Wildcard classpaths are available in JDK 6+, the issue instead is that you can't specify both -jar and -cp at the same time. Putting your jars in your ext directory, IMO, is a pretty horrible idea. Oct 22, 2012 at 19:15
  • @DaveNewton Yeah and this question is about executing a jar with its dependencies.
    – Jagger
    Oct 22, 2012 at 19:16
  • Yeah and you can do that with a wildcard classpath. Filling your ext directory affects everything you run, and is dangerous if you ever run anything else. It's a Maven project; use Maven. Oct 22, 2012 at 19:18
  • @DaveNewton And how would you want to do it? Put the wildcard and then fully qualified name of the class which contains the main method? The whole idea of having an executable jar file is to avoid putting fully qualified name while executing and instead having it in the manifest file.
    – Jagger
    Oct 22, 2012 at 19:21
  • 1
    If by "cumbersome" you mean "will likely break everything else in the world", sure. Oct 22, 2012 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.