Weixiang Guan
Weixiang Guan

Reputation: 487

Run jar in command line on Windows (Classpath)

I know this is a very old and common question already, but still I cannot get it work!

I am developing a Java program on Windows 7 using Intellij, I add file output of artifact to produce a jar when compiling. The program works fine in the IDE, and now I want to run it without the IDE, in command line.

The directory structure is like the following:

Project

-out

--ProjectName

---project.jar

-lib (under lib there are all the jar libraries used in the project)

-..

(hope you can understand the structure illustrated above)

So I enter the directory where the project.jar is in, and type the following:

java -cp ..\..\..\lib -jar project.jar

and I get the error message of the exception NoClassDefFoundError, but indeed the class is contained in a jar under the lib directory.

This should be pretty simple, but I cannot make it work.

Please help! Much appreciated!

Upvotes: 1

Views: 8053

Answers (4)

Srujan
Srujan

Reputation: 11

I faced a similar problem.. And figured out a solution..Im not sure whether this is already given..If so ignore this.. The scenario is based on Maven package file(jar) to be run along with class name for JAVA learners. So for this use the command :java -classpath jarfile packagename.classname For eg : java -classpath target\MavenTest-1.0-SNAPSHOT.jar org.first.edu.App

Upvotes: 1

tusharagrawa
tusharagrawa

Reputation: 381

First of all make sure you are running the command from the directory in which your jar is kept then enter this command in the command prompt:

java -cp "<-Your Jar Name:Jar Lib>" <-Your Package name and main class->

For example :-

java -cp "project.jar:lib/*" com/foo/bar/main

Upvotes: 0

Weixiang Guan
Weixiang Guan

Reputation: 487

I found the problem and the solution. If I run the project.jar with the -jar parameter, the -classpath parameter is ignored, vice versa. So I added the classpath in the .jar file (done through the classpath textbox in Intellij), and tried several times. Finally I found out, I need to specify the classpath beginning with a dot!!! Which means the relative path starts from the current directory, and goes further. It is quite surprising, since I believe a mandatory dot to indicate the current directory is used only in Linux (while I am using Windows). Seems Java handles this differently.

Upvotes: 0

StormeHawke
StormeHawke

Reputation: 6207

try java -cp ..\..\..\lib\*.jar -jar project.jar if you don't want to list all the jars individually

Upvotes: 0

Related Questions