0

I have program in Java in path: C:\...\MyProgram. This program have some dependencies to others *.jar files. I would run it using cmd. So what I do:

in cmd I write cd C:\...\MyProgram\bin and then java -cp C:\...\MyProgram\*;. main.Main. It is working. But now I exported MyProgram to jar file. Could you tell me how can I run this now? So I have file MyProgram.jar with these same dependencies. How run it by using cmd?

3 Answers 3

2

Folders and archive files

When classes are stored in a directory (folder), like /java/MyClasses/utility/myapp, then the class path entry points to the directory that contains the first element of the package name. (in this case, /java/MyClasses, since the package name is utility.myapp.)

But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this:

% java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool

Found in http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html

1

You need to add a Class-Path entry to the manifest file (META-INF/manifest.mf) inside the jar:

Class-Path: /C:/.../MyProgram/ .

This assumes that there are dependent classes under C:/.../MyProgram/, not jar files.

You should also add an entry for the Main-class:

Main-Class: main.Main

Then just execute your jar as

java -jar MyProgram.jar

4
  • Could you tell me where should I exactly add file manifest.fm? I can not find it.
    – Jacob
    Commented Jul 24, 2014 at 10:09
  • It should be within the jar (MyProgram.jar) which you can open like a zip file. The file should be located at META-INF/manifest.mf
    – JamesB
    Commented Jul 24, 2014 at 10:25
  • Ok, there is. And what should I do? There is only line Main-Class: main.Main. So I have to add line Class-Path: /C:/.../MyProgram/ ? And what then? If I will execute jar file, there is no changes...
    – Jacob
    Commented Jul 24, 2014 at 12:24
  • Remember to include the . character in the Class-Path too? Ensure the file is saved within the jar and then try executing as I instructed.
    – JamesB
    Commented Jul 24, 2014 at 13:38
0

Set the path of JAR file in your classpath and then execute the other JAR file.

To add JAR using eclipse.

  1. Right click on project -> properties

  2. Java Build Path -> Click add external JARs.

  3. This will add JAR to your classpath.

enter image description here

5
  • In .classpath file I have something like: <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry exported="true" kind="lib" path="oneLib-0.3.2.jar"/> <classpathentry exported="true" kind="lib" path="lastLib-1.2.jar"/> <classpathentry kind="output" path="bin"/> </classpath> Could you tell me what should I add here?
    – Jacob
    Commented Jul 24, 2014 at 10:02
  • <classpathentry kind="lib" path="path_of_other_jar"/> you can also use eclipse to do this for you.
    – Vishrant
    Commented Jul 24, 2014 at 10:10
  • @Jacob check edited answer, to add path of your jar to classpath.
    – Vishrant
    Commented Jul 24, 2014 at 10:13
  • I know how to add jar files. So I have all of them. If I write in cmd: java -jar MyProgram.jar i see:exception in thread main java.lang.NoClassDeffFundError
    – Jacob
    Commented Jul 24, 2014 at 10:21
  • check if JAR path is added in your .classpath file located in root folder of project.
    – Vishrant
    Commented Jul 24, 2014 at 10:25

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