1
votes

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?

2
It's not part of a package.dwjohnston
Why are the two classes in different directories?Andrew Thompson
Well... I'm playing around with working out how I'd compile for something. Though the something similar does use packages.dwjohnston
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

2 Answers

2
votes

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.

0
votes

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