I am at my wits end trying to figure out how to specify the main-class in my ant build file. Perhaps I am not understanding where it looks. I am working under the understanding that it is specified in the manifest file. Since it cannot find main, then I have to assume that what I am specifying for Main-Class is wrong. When I open the jar file I can see the java file with the main class in it. It is in the top level of the jar file. Thus it seems to me that the Main-Class is either '.' or the name of that file without the .java. Neither work so I have no clue what to specify. Can someone please help. TIA.
First, your main class should not be in the top level directory. You can runn into all kinds of problems using "packageless" classes. You need to go back and add a package declaration to your main class, and also move the java source file for it to the correct directory. Then the Main-Class in the manifest should be the full class name, including the package.
Thanks for the reply. This is a little confusing since it is not a "packageless" class. Based on what you say, I am wondering if maybe I have the build.xml in the wrong directory. I put it in the src directory because it was not clear to me where it should go. Based on what you say I believe that is my situation. Should it go in the next directory up so that it is at the same level as src or go 2 levels up so it is in the same directory as the package?
Perhaps it would help if I posted the tree Eclipse built for me:
"packageless" class is a misnomer. All Java classes belong to some package or other, but if you don't explicitly declare one, the class is compiled into the default package. And, as Peter mentioned, that's a very bad idea.
On small Ant projects, the usual layout would be to put the build.xml in the project's root directory, create a "src" directory to hold the java source tree and a "classes" directory to hold the compiled classes. It's important to keep source files separate from the set of synthesized files (compiled classes), since otherwise you risk accidentally erasing important things when you clean the project.
In Java, the package name is a critical part of both the source and compiled filenames. While you can compile a file whose package name is "com.javaranch.project1" and whose location is directory under "src", the output class will be placed under "classes/com/javaranch/project". And you may get a compiler warning for not placing the source class in "src/com/javaranch/project".
More than one class in a project can contain a "public static Main(String args)" method. There is no language-defined way to determine which class should be used as the actual main class. Therefore you must explicitly define that main class, which you do either on the command line, or via the Main-Class manifest in the jar you create using the "classes" directory as its contributor. And, since there can be classes named "MyMainClass" in more than one package (although I don't recommend it!), you must provide the fully-qualified classname (including absolute package) in your manifest. Like so:
An IDE is no substitute for an Intelligent Developer.
Joined: Feb 03, 2012
As an ANT noob I'm afraid I am not following you at all. Since I am using Eclipse perhaps it is hiding too much from me but I don't see how what you are saying applies to my situation. I have the tree as shown, I've moved the build.xml around and make appropriate adjustments on each try. However, the bottom line is that no matter what I use for Main-Class in the manifest, I always get the same error. I simply have not idea what to use even when I look in the jar file and can see where that class is.
I think I see the issue. The "basedir" you are setting is not correct, it shouldn't be the base directory of yuou project, but rather the base directory of where your compiled classes are located. In your compile task, where do you compile your source files to? It is that directory that you want to use for basedir. In the example I gave, basedir would be "classes".
Joined: Feb 03, 2012
That is a good question. I don't know where Eclipse compiles it too but I suspect it is 'bin'. However, since I am using ANT, isn't that where I specify it? Is that a parameter missing in my build.xml and if so what/where is it? I would guess that it and basedir must agree.