Making a jar with multiple packages
Hi all,
I have a program with a few packages. I have tried to put them in a jar but keep getting a NoClassDefFoundError MyProg/ui/Maiin
I have copied all the source files into MyProg (keeping directory structure);
MyProg/ui
MyProg/data
MyProg/fileIO
the main class is ui.Main
I have put mainClass in MyProg (with line Main-Class: ui.Main and a carriage return)
I made my jar with;
jar cmf .\MyProg\mainClass MyProg.jar MyProg
and this works fine, but when I run;
java -jar MyProg.jar
The NoClassDefError occurs.
Any ideas?
> Hi all,
>
> I have a program with a few packages. I have tried
> to put them in a jar but keep getting a
> NoClassDefFoundError MyProg/ui/Maiin
>
> I have copied all the source files into MyProg
> (keeping directory structure);
>
If you are only including source files in your jar, nothing will execute.
> MyProg/ui
> MyProg/data
> MyProg/fileIO
>
> the main class is ui.Main
>
Then when you jar the Main.class file, it needs to be in the jar as ui/Main.class. It must not be just Main.class or MyProg/ui/Main.class
> I have put mainClass in MyProg (with line Main-Class:
> ui.Main and a carriage return)
>
> I made my jar with;
> jar cmf .\MyProg\mainClass MyProg.jar MyProg
> and this works fine, but when I run;
What do you get when you execute "jar tf MyProg.jar?" Do you see the right class files inside the right directory?
>
> > java -jar MyProg.jar
>
>
> The NoClassDefError occurs.
>
> Any ideas?
Please post the full error message.
Also, does the program execute outside the jar?
Thanks for your reply atmguy,
I did mean the .class files were in the various folders, not the source files as I have said. Sorry.
Yes Main.class is in MyProg/ui/Main.class
When I execute
jar tf MyProg.jar
I do see all of the class files in the correct directory structure; e.g.
ui/Main.class
data/Records.class
etc
Full error message;
"Exception in thread "main" java.lang.NoClassDefFoundError: MyProg/ui/Main
any ideas?
I found a way to fix it (amazing how explaining something to someone else helps eh?)
I go into the MyProg directory.
The mainClass file has;
Main-Class: ui.Main
(not MyProg.ui.Main)
and execute
jar cmf mainClass MyProg.jar *
and hey presto everything works.
Would still be nice to be able to do this from above the MyProg directory though.