Nick Brunt
Nick Brunt

Reputation: 10047

How do you specify a main class in a manifest file when compiling from the parent directory?

My directory structure is like this:

> makefile
> app.jar
> src
    > Main.java
    > Main.class
    > some other classes
    > manifest.txt

I compile from the makefile with these commands:

javac src/*.java
jar cvfm app.jar src/manifest.txt src/*.class

manifest.txt contains this:

Main-Class: Main

When I run java -jar app.jar from the top directory I get this error:

$ java -jar app.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Main. Program will exit.

I tried changing the manifest to say Main-Class: src.Main but that didn't work either. This is probably very simple but I'm finding Google doesn't provide any simple solutions.

Upvotes: 1

Views: 2405

Answers (2)

Pavan
Pavan

Reputation: 1289

You are getting NoClassDefFoundError exception, might be class-path is not set properly.

Thanks, Pavan

Upvotes: 0

Pavan
Pavan

Reputation: 1289

Please find the answer from Oracle

http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Please find the sample code:-

package oata
public class HelloWorld {
 public static void main(String[] args) {
    System.out.println("Hello World");
 }
 }

Create a folder to place the .class file Then compile the and run the file.

 `md build\classes
   javac -sourcepath src -d build\classes src\oata\HelloWorld.java
  java -cp build\classes oata.HelloWorld`


  `echo Main-Class: oata.HelloWorld>myManifest
     md build\jar
    jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .
    java -jar build\jar\HelloWorld.jar`

Thanks Pavan

Upvotes: 1

Related Questions