I have a build setup that requires that my classes be put into a folder in a subdirectory of my current directory. I.E., if I'm at '.', then the classes could be in './somewhere_else/'
The problem is that when I do this I can't set a value for Main-Class that can find the main class. I have no problem when I build the jar from files at '.', but all the following atte开发者_C百科mpts for main class result in java.lang.NoClassDefFoundError's:
Main-Class: ClassName
Main-Class: FolderName.ClassName
Main-Class: FolderName/ClassName
What should I be using?
The classes inside your jar have to be in the same directory structure as your package structure.
- So, if you want to have your class
ClassName
(without any package) to be the main class, it has to be in the root directory of your jar. - If you want to have the class in the directory
FolderName
, this same name has to be the package name, meaning the first (non-empty non-comment) line of your source file should bepackage FolderName;
.
If your problem is only the build setup - this is not a problem, the folder-layout of your directories outside the jar does not necessarily have to be the same as the layout inside your jar. But it still helps if the folder-structure fits to the package-structure (with maybe additionally directories above).
I think you should reference Main-Class simply by the class name (without directory structure). What will help you solve this problem is what you put as the classpath. Make sure the directory of Main-Class is in the classpath.
If your ClassName
resides in package FolderName
, then, it should be:-
Manifest-Version: 1.0
Main-Class: FolderName.ClassName
Class-Path: <third-party>.jar <another-third-party>.jar
They are case-sensitive, by the way.
精彩评论