Home » Language IDEs » Java Development Tools (JDT) » Program runs, but cannot export to JAR
Program runs, but cannot export to JAR [message #250536] |
Wed, 09 January 2008 17:52  |
Eclipse User |
|
|
|
Originally posted by: Seventhlost.aol.com
I made a java program in Eclipse that uses two jar files that I imported
as libraries. I can run the program inside Eclipse, but now I want to
export the program as a Jar file. So I go through the usual steps of
Export -> JAR File -> Select the following:
1. Export generated class files and resources
2. Compress the contents of the Jar File
Then I select the location where I want to save the JAR file and hit the
next button.
I check only "export class files with compile warnings", I do get some
compile warnings. Then I select the option to generate the manifest file
and I select the class with the static main function.
Error message is:
Could not find the main class. Program will exit.
My manifest file looks like:
Manifest-Version: 1.0
Main-Class: DriverConnection
My DriverConnection class is the only class that I made, it looks like
this:
public class DriverConnection {
public DriverConnection() {
}
<...more functions...>
public static void main(String[] args) {
DriverConnection drivercon = new DriverConnection();
drivercon.execute();
}
}
This works fine when executing in eclipse, but when I export to JAR file,
I get the error mentioned above. Also, I am using the default package
name. Any ideas?
|
|
| | |
Re: Program runs, but cannot export to JAR [message #250548 is a reply to message #250544] |
Wed, 09 January 2008 20:02   |
Eclipse User |
|
|
|
Originally posted by: wharley.bea.com
"Samantha" <Seventhlost@aol.com> wrote in message
news:7bbf537586908bb0b6fab8fc00ff5370$1@www.eclipse.org...
>I opened the JAR file with WinZip and I see the two class files (one for
>DriverConnection and another one for its inner class), along with two other
>jar files, and also the Manifest.mf file. Which I pasted the contents
>above. I have not tried executing from the command line. I executed in
>Eclipse by selecting Run As -> Java Application.
I'm going to assume, since Eclipse created the manifest and the jar file,
that the manifest.mf file is in the META-INF directory. But it wouldn't
hurt to confirm that.
I'm having a little trouble understanding what is and isn't working. You
said earlier that "This works fine when executing in eclipse, but when I
export to JAR file, I get the error mentioned above." But now you're saying
that the error message you were seeing was when you were executing in
Eclipse, not at the command line? Can you clarify?
I'm suspecting a problem with your launch configuration in Eclipse, given
that (from the next part of your post) it seems that at the command line
java is able to find the main class and execute it.
> jxl is one of the two jar files mentioned above that are included in the
> JAR file that I am trying to execute...is there a problem with including
> one JAR inside of another? Strange how I never saw any of these errors
> when running inside Eclipse.
That's correct, you can't execute nested jars. They have to all be unpacked
and on the runtime classpath. That's a Java limitation, not an Eclipse
limitation per se. (Eclipse plug-ins can contain nested jars, because
Eclipse is smart enough to unpack the plug-in contents into a cache
directory on the fly.)
I think the reason you didn't see them when running in Eclipse is that
Eclipse is running your application from the build classpath and binary
output folder, rather than from the jar file.
|
|
| |
Re: Program runs, but cannot export to JAR [message #250555 is a reply to message #250552] |
Wed, 09 January 2008 21:06   |
Eclipse User |
|
|
|
Originally posted by: wharley.bea.com
"Samantha" <Seventhlost@aol.com> wrote in message
news:9cceee99cb96acc459a0a88e3c0ee1b0$1@www.eclipse.org...
> The manifest file is in the META-INF directory. Sorry about the last
> post, I can see how it was confusing. I meant to say that I have never
> had any issues executing the program inside eclipse (by using the Run
> As... command and executing as a Java application). This has always
> worked. Executing the jar file from and command line does not work, as I
> showed with two quotes of trying to execute the jar from the command line.
> So Eclipse always works, command line never works.
So, when did you get the "can't find main method" error? That's the one
that I don't understand; the missing classes errors are explained by the
nested jar problem.
> I did not know that one cannot include a JAR inside of a JAR. And if this
> is not allowed, it would have been nice if Eclipse extracted the inner
> JARs. Is there an option that I needed to select to include the contents
> of the two JAR files rather than the actual JAR files -- extraction?
Sometimes it makes sense to nest jars; for instance, if some of your code
(e.g., your main routine) is capable of extracting and caching the jars.
You might actually want to think about an option like that. Or, there are
products such as "fatjar" (Google for it) that can do roughly the same
thing - that might be a good solution for you, though I've not tried it.
In some cases, licensing agreements don't permit the contents of a jar file
to be unpacked and repackaged "flat" into your product; for instance, this
makes it very hard to determine which licensing agreement applies to which
file, if the jar came from some other vendor that had their own licensing
agreement. (In fact, sometimes the licensing agreement doesn't permit any
sort of packaging at all - so make sure the jars you're repackaging are
legal.)
Usually Java-based products are either distributed as a collection of jars,
or as a single meta-jar that contains the others and has some sort of
installer or self-extraction code.
I do not believe Eclipse has any mechanism to automatically unpack and
repack jars contained within a project.
|
|
|
Re: Program runs, but cannot export to JAR [message #250572 is a reply to message #250555] |
Thu, 10 January 2008 14:08   |
Eclipse User |
|
|
|
Originally posted by: Seventhlost.aol.com
All I really know is that it works in Eclipse. I have never had any
issues running from inside eclipse. However, if I go outside of Eclipse
and open a command prompt, navigate to the directory, and type in java
-jar myjar.jar ClassName, I get this error message:
Exception in thread "main" java.lang.NoClassDefFoundError:
jxl/read/biff/BiffException
Where jxl is a jar file inside of the "myjar.jar" file that I tried to
execute. When I double click on the jar file, I get the main class not
found error. I don't really know what all this means. I just think that
it has something to do with those two jar files which I included inside of
the jar that I am trying to execute.
So how do I make this all work? It seems that Java made the compiling
process easier, compared to something like C++, however, executing can be
much more difficult. I just want Eclipse to produce something that I can
execute. I am not sure how to handle the two JAR libraries needed for my
program.
|
|
| |
Re: Program runs, but cannot export to JAR [message #250579 is a reply to message #250572] |
Thu, 10 January 2008 22:23   |
Eclipse User |
|
|
|
Originally posted by: smustard.cinci.rr.com
If you want to run 'java -jar myjar.jar ClassName' then you must package
all the dependent classes within your myjar.jar. This is due to issues
with the way java handles its classpath loading.
Sandy
Samantha wrote:
> All I really know is that it works in Eclipse. I have never had any
> issues running from inside eclipse. However, if I go outside of Eclipse
> and open a command prompt, navigate to the directory, and type in java
> -jar myjar.jar ClassName, I get this error message:
>
> Exception in thread "main" java.lang.NoClassDefFoundError:
> jxl/read/biff/BiffException
>
> Where jxl is a jar file inside of the "myjar.jar" file that I tried to
> execute. When I double click on the jar file, I get the main class not
> found error. I don't really know what all this means. I just think
> that it has something to do with those two jar files which I included
> inside of the jar that I am trying to execute.
>
> So how do I make this all work? It seems that Java made the compiling
> process easier, compared to something like C++, however, executing can
> be much more difficult. I just want Eclipse to produce something that I
> can execute. I am not sure how to handle the two JAR libraries needed
> for my program.
>
|
|
| | | |
Re: Program runs, but cannot export to JAR [message #251757 is a reply to message #250572] |
Sat, 01 March 2008 11:25  |
Eclipse User |
|
|
|
Originally posted by: thunderaxiom.gmail.com
Samantha skrev den 10-01-2008 20:08:
> All I really know is that it works in Eclipse. I have never had any
> issues running from inside eclipse. However, if I go outside of Eclipse
> and open a command prompt, navigate to the directory, and type in java
> -jar myjar.jar ClassName, I get this error message:
>
> Exception in thread "main" java.lang.NoClassDefFoundError:
> jxl/read/biff/BiffException
>
> Where jxl is a jar file inside of the "myjar.jar" file that I tried to
> execute. When I double click on the jar file, I get the main class not
> found error. I don't really know what all this means. I just think
> that it has something to do with those two jar files which I included
> inside of the jar that I am trying to execute.
>
> So how do I make this all work? It seems that Java made the compiling
> process easier, compared to something like C++, however, executing can
> be much more difficult. I just want Eclipse to produce something that I
> can execute. I am not sure how to handle the two JAR libraries needed
> for my program.
>
Either put the supportive jar file next to the jar you are creating and
put its name in the Class-Path line in the manifest (which the exporter
allows you to do), or use the Fat Jar plugin which uses the one-jar
classloader magic thing to allow using jars inside the jar you are creating.
--
Thorbjørn
|
|
|
Goto Forum:
Current Time: Tue Apr 22 11:37:29 EDT 2025
Powered by FUDForum. Page generated in 0.04797 seconds
|