1
votes

I have to create a JAR file (without using any IDE)

That's what I'm doing:

I got a folder named Project, this is the main one, in this one i two folders and a pdf file.

one named ClassFiles that contains all the ".class" files of my project

the other one named JavaFiles that contains all the ".java" files.

the file is a relation that i have to include, nothing relevant though, and is called Info.pdf.

The error i have is this:

My main class (is redundant, i mean the starting one, with "public static void main(String[] args)") is called Main

the command line i'm using is:

jar -cfe Project.jar ClassFiles.Main JavaFiles ClassFiles INFO.pdf

i get the error Could not find the main class: ClassFiles.Main. Program will exit

PS: in the ClassFiles folder i have a lot of classname$1, classname$2 files. i don't know if it's relevant. I even have the Main with and another Main$1.

I need to give this JAR tomorrow, so i'm quite desperate.

Thank you in advance!

Exception in thread "main" java.lang.NoClassDefFoundError: ClassFiles/Main (wrong name: Main)

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    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: ClassFiles.Main. Program will exit

@erikson: ClassFiles is just a folder i made to contain all the .class, nothing more... there is no package in what i did (it was the default one when i used Eclipse)

@ Dave Newton: sorry man, (first of all thank you), could u be a little more specific? i'm not understanding what i should do. i'm not really good with this stuff

ah, if you mean that the package is not called Project but Progetto is just becouse i translated it in english. not sure if that's what you are referring too.

I have the Main.class in the ClassFiles directory and Main.java in the JavaFiles dir

That's exactly what i did and the result:

ascal@AscaL ~/Desktop/Progetto $ jar -cfe Progetto.jar ClassFiles.Main ClassFiles JavaFiles Info.pdf

ascal@AscaL ~/Desktop/Progetto $ java -jar Progetto.jar

Exception in thread "main" java.lang.NoClassDefFoundError: ClassFiles/Main (wrong name: Main) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:634) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) at java.net.URLClassLoader.access$000(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:212) 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: ClassFiles.Main. Program will exit.

//Code of Main.java

http://pastebin.com/Xh9emcYH

3
Does your Main class declare itself to be in the ClassFiles package? I.e., first line of code should be package ClassFiles; (Well, actually you should pick a legitimate package name and create the appropriate directory structure for it, but let's do one thing at a time.) - erickson
As I said in my answer, and erickson says here, your package is wrong (as is your original question--you do not get the error when running jar -cfe). If you're going to package it up in the ClassFiles directory, it should be in the ClassFiles package. - Dave Newton
Do what @erickson said and put package ClassFiles; at the top of your Main class. - Dave Newton
No, I mean "put package ClassFiles; at the top of your Main class". (Although also as @erickson said, that's not a very standard package name, but one step at a time.) - Dave Newton
You are using a package, you just don't understand it. When you specify the entry-point of (-e option of jar) as ClassFiles.Main, you are saying that the class Main is in the ClassFiles package. And the corresponding .class file must be in a folder named ClassFiles. And any other classes that end up in that directory with Main should declare the ClassFiles package too. - erickson

3 Answers

3
votes

You get that error when you run the jar command? Works fine for me.

$ tree
.
├── ClassFiles
│   └── Main.class
├── JavaFiles
│   └── Main.java
└── Project.jar
$ jar -cfe Project.jar ClassFiles.Main JavaFiles ClassFiles
$ jar -tf Project.jar
META-INF/
META-INF/MANIFEST.MF
JavaFiles/
JavaFiles/Main.java
ClassFiles/
ClassFiles/Main.class

Of course, if you run it via java -jar Project.jar, and the Main class isn't in the ClassFiles package, it'll break. What package is the Main class in?

$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Created-By: 1.6.0_26 (Apple Inc.)
Main-Class: ClassFiles.Main
1
votes

Create a manifest file in the base folder containing:

Manifest-Version: 1.0
Main-Class: ClassFiles.Main

Then run:

jar cvfm Project.jar manifestFile.txt ClassFiles/*

Hope it helps :)

0
votes

To run a jar file, you should use the prescribed directions given in the Java Sun Developing Basics website.