1

Here's what I've got.

I've got my 'MyJava' folder which everything is contained in.

MyJava/src/a/HelloWorld.java
MyJava/src/b/Inner.java
MyJava/bin/
MyJava/manifest.txt

HelloWorld.java:

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World");

        Inner myInner = new Inner(); 
        myInner.myInner(); 
    }
}

Inner.java:

public class Inner {

    public void myInner() {
        System.out.println("Inner Method");
    }
}

Manifest.txt:

Main-Class: HelloWorld

First I compile the .javas to .class:

javac -d bin src/a/HelloWorld.java src/b/Inner.java

Now I put these into a .jar file jar cvfm myTwo.jar manifest.txt bin/*.class

now I try run the jar: java -jar myTwo.jar

And I get:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
...
Could not find the main class: HelloWorld. Program will exit.

I know this is a pretty simple problem, what am I missing?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
  • It's not part of a package. – dwjohnston May 15 '13 at 07:32
  • Why are the two classes in different directories? – Andrew Thompson May 15 '13 at 07:36
  • Well... I'm playing around with working out how I'd compile for something. Though the something similar does use packages. – dwjohnston May 15 '13 at 07:38
  • 1
    Packages in Java source on the local file system usually correspond to the directory in which the source is kept. So if a class had package `pkg.name` & name `MyClass` it should be in path a `pkg/name/MyClass.java`. I recommend if your classes are both in the same package (the default package it seems) they should be in the same directory. There are ways to do it differently, but I suggest you stick with the simplest way for the moment. – Andrew Thompson May 15 '13 at 08:03

2 Answers2

2

If you examine the files inside your .JAR you will notice that your compiled classes are inside the bin directory (and therefore cannot be found, since your manifest references a class in the top level).
Change your jar... command like this:

jar cvfm myTwo.jar manifest.txt -C bin .

See also the "Creating a JAR File" section of the Java Tutorial.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
0

One of the solutions is to add the following line to the manifest.txt

Class-Path: bin/

Then you can use 'your' command for the jar creation:

jar cvfm myTwo.jar manifest.txt bin/*.class
Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68