• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"Could not find main class" problem in jar file

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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:

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally got around to trying this and IT WORKED!!! Thanks, I owe you one.
 
reply
    Bookmark Topic Watch Topic
  • New Topic