-2

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

Tim Post
  • 33,371
  • 15
  • 110
  • 174
AscaL
  • 183
  • 3
  • 3
  • 11
  • 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 Sep 08 '11 at 23:01
  • 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 Sep 08 '11 at 23:06
  • Do what @erickson said and put `package ClassFiles;` at the top of your `Main` class. – Dave Newton Sep 08 '11 at 23:12
  • 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 Sep 08 '11 at 23:15
  • 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 Sep 08 '11 at 23:17
  • just to be clear, you mean at the top of the Main.class or of the Main.java file? – AscaL Sep 08 '11 at 23:17
  • The Java file. It would be... unwise to edit the class file as text. – Dave Newton Sep 08 '11 at 23:18
  • i edited it, still got the error. i edited in this way: package ClassFiles; //class body there – AscaL Sep 08 '11 at 23:24
  • Put the code in your question, or at this point, pastebin or gisthub, and provide a link. You're missing some basic Java stuff that is probably better discussed elsewhere. – Dave Newton Sep 08 '11 at 23:25
  • there are like 16 files beside the Main... and the code is working, i just can't make the JAR. should i really poste ALL the code? – AscaL Sep 08 '11 at 23:27
  • Did you re-copy the class file to the ClassFiles directory and re-jar? It doesn't sound like it. – Dave Newton Sep 08 '11 at 23:27
  • No, the rest is irrelevant, only the package, first few lines of Main.java, and whether or not you re-moved the .class file and re-jarred. If you did, it cannot fail--I have the exact same file layout here and it works fine. – Dave Newton Sep 08 '11 at 23:28
  • i took the files form the Project directory, both the .class and the .java. what do you mean by re-jarred? beside is there any chance we can talk in a better way? maybe you got skype? (if you are comfortable ofc). ah well i got like another 16 files in there. – AscaL Sep 08 '11 at 23:32
  • You have to jar the files again, because you changed them. https://gist.github.com/1205093 – Dave Newton Sep 08 '11 at 23:32
  • if you can wait one minute i can maybe like you my folder, so you understand what i'm saying... beside thank you alot, you are really kind :) – AscaL Sep 08 '11 at 23:37
  • Now recompile, copy the class file into the ClassFiles directory, re-jar everything, and it should work. – Dave Newton Sep 08 '11 at 23:40
  • i was thinking, should i add the "package" to every .java file i got? – AscaL Sep 08 '11 at 23:43
  • Yes, if you have other source files in the JavaFiles directory; they're all in the same package. – Dave Newton Sep 08 '11 at 23:44
  • maybe it was that. now i'll try. anwyay is still not woking. – AscaL Sep 08 '11 at 23:45
  • still not working. i don't know what to do T_T – AscaL Sep 08 '11 at 23:48
  • Look at my gist: you have more classes, but that's not relevant. If you compile them all, move them to the correct directory, re-jar everything, it'll work. I'd recommend some very basic Java compilation/package/directory structure tutorials, but this isn't the right place to continue this. Start with my example on your machine--start with something simple, not all at once. – Dave Newton Sep 08 '11 at 23:48
  • Look i'm sending you the link to the folder i'm currently using to do the work. i modified all files adding the package: http://dl.dropbox.com/u/27045853/Project.tar.gz – AscaL Sep 08 '11 at 23:52
  • do you mean recompile using java? – AscaL Sep 08 '11 at 23:53
  • How else would you recompile your Java source code? – Dave Newton Sep 08 '11 at 23:57
  • right sorry. doing it now. thx for the patience man – AscaL Sep 09 '11 at 00:01
  • WOW IT WORKED. I LOVE YOU. thank you man, thank you very very much :D now how do i give you reputation? first time on this site – AscaL Sep 09 '11 at 00:02
  • well i'm going to sleep, sorry i can't give you reputation or anything. but i really apriciated what you did, thank you very very much again. i wish u a good life :) – AscaL Sep 09 '11 at 00:17
  • You accept and vote up my answer :) Glad you worked it out. – Dave Newton Sep 09 '11 at 00:19
  • unfortunatly i need 15 rep to give you +1 to the answer, but i accepted it, hope is enaught :) thank you again! – AscaL Sep 09 '11 at 00:23
  • It doesn't look like you accepted his answer. I did upvote it though. – erickson Sep 09 '11 at 00:41
  • probabily i don't know how to do it, i went on his answer and clicked "was this post useful to you" YES. more than that i dunno what i can actually do. anyway thank you erickson for +repping him :) – AscaL Sep 09 '11 at 15:00

3 Answers3

3

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
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

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 :)

Gigab0rt
  • 302
  • 4
  • 14
0

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

Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36
  • That version of the docs is too old to be helpful; doesn't even cover the command-line arguments the OP is using. – Dave Newton Sep 08 '11 at 23:09