aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark ""Could not find main class" problem in jar file" Watch ""Could not find main class" problem in jar file" New topic
Author

"Could not find main class" problem in jar file

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5842
    
    7

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.

Example: lets say you currently have:

src/HelloWorld.java
classes/HelloWorld.class

and you JAR file contains:

HelloWorld.class
META-INF/MANIFEST.MF

What you really want is something like:

src/org/dennis/HelloWorld.java
classes/org/dennis/HelloWorld.java

and your JAR will contain:

org/dennis/HelloWorld.java
META-INF/MANIFEST.MF

and your <Main-Class> value will be:



JBoss In Action
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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:

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

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


Customer surveys are for companies who didn't pay proper attention to begin with.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5842
    
    7

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".
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
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.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5842
    
    7

Yes, you would specify the build target directory in the javac task in your Ant build script. And that is what you should set basedir in the jar task to. Example:

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I finally got around to trying this and IT WORKED!!! Thanks, I owe you one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "Could not find main class" problem in jar file