Skip to main content



      Home
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 Go to next message
Eclipse UserFriend
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 #250540 is a reply to message #250536] Wed, 09 January 2008 17:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Samantha" <Seventhlost@aol.com> wrote in message
news:d18b84614cc3ac63793c65d5f1e5b6de$1@www.eclipse.org...
> 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: [etc.]


It looks like you did the right steps. Have you looked at the contents of
the resulting .jar file (you can use WinZip or any such program, a .jar file
is just a renamed .zip file) to see if the class is actually in there, and
in the correct relative path (i.e., at the root)? Maybe it wasn't checked
in the export wizard.

Also, exactly how are you launching the program? What is the command line?
Re: Program runs, but cannot export to JAR [message #250544 is a reply to message #250540] Wed, 09 January 2008 19:41 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Seventhlost.aol.com

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 did not do
anything special for that, I do not pass in any arguments. Interesting, I
just tried executing the JAR file built by eclipse from the command line
and I get the following errors, tried 2 ways:

java -jar SortTestTime.jar DriverConnection

Exception in thread "main" java.lang.NoClassDefFoundError:
jxl/read/biff/BiffException

and

java -cp SortTestTime.jar DriverConnection
Exception in thread "main" java.lang.NoClassDefFoundError:
jxl/read/biff/BiffException

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.
Re: Program runs, but cannot export to JAR [message #250548 is a reply to message #250544] Wed, 09 January 2008 20:02 Go to previous messageGo to next message
Eclipse UserFriend
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 #250552 is a reply to message #250548] Wed, 09 January 2008 20:35 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Seventhlost.aol.com

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.

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?
Re: Program runs, but cannot export to JAR [message #250555 is a reply to message #250552] Wed, 09 January 2008 21:06 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 #250575 is a reply to message #250572] Thu, 10 January 2008 17:11 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Samantha" <Seventhlost@aol.com> wrote in message
news:edb64f644e2e3f0d9e4ea4b5025e21ae$1@www.eclipse.org...
> 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.


Eclipse is a pretty good tool for writing Java code, but it is not an
application generator. You still need to know how to write and package a
Java application. Eclipse has no way of knowing how you intend to use the
jar you tell it to produce, and there are (as I mentioned) cases where
nesting a jar is the right thing.

I would recommend taking a look at fatjar. I would also recommend looking
closely at how other complex java applications are packaged and distributed.
(E.g., Eclipse is packaged as a .zip file, that the user manually unpacks
during "installation".)
Re: Program runs, but cannot export to JAR [message #250579 is a reply to message #250572] Thu, 10 January 2008 22:23 Go to previous messageGo to next message
Eclipse UserFriend
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 #250582 is a reply to message #250575] Fri, 11 January 2008 03:44 Go to previous messageGo to next message
Eclipse UserFriend
Walter Harley wrote:

>"Samantha" <Seventhlost@aol.com> wrote in message
>news:edb64f644e2e3f0d9e4ea4b5025e21ae$1@www.eclipse.org...
>
>
>>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.
>>
>>
>
>
>Eclipse is a pretty good tool for writing Java code, but it is not an
>application generator.
>
Using 3.4 M4 try File > Export... > Runnable JAR file

Dani

> You still need to know how to write and package a
>Java application. Eclipse has no way of knowing how you intend to use the
>jar you tell it to produce, and there are (as I mentioned) cases where
>nesting a jar is the right thing.
>
>I would recommend taking a look at fatjar. I would also recommend looking
>closely at how other complex java applications are packaged and distributed.
>(E.g., Eclipse is packaged as a .zip file, that the user manually unpacks
>during "installation".)
>
>
>
>
>
Re: Program runs, but cannot export to JAR [message #250692 is a reply to message #250582] Tue, 15 January 2008 15:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Seventhlost.aol.com

Thanks Dani, I downloaded Eclipse 3.4 and it knew how to create a runnable
JAR file. It automatically deflates all the reference JAR files and
builds everything into one big JAR file. The only problem that I had, and
there is probably a way around this, is that my program reads a text file.
Eclipse did not include the text file, it only included the the binary
files. I just copied the text file over into the same folder as the JAR,
then everything worked perfectly! Eclipse is really a great tool.
Re: Program runs, but cannot export to JAR [message #250695 is a reply to message #250692] Tue, 15 January 2008 16:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Samantha" <Seventhlost@aol.com> wrote in message
news:bf1409ae69c3b10d86a349a11231176e$1@www.eclipse.org...
> Thanks Dani, I downloaded Eclipse 3.4 and it knew how to create a runnable
> JAR file. It automatically deflates all the reference JAR files and
> builds everything into one big JAR file. The only problem that I had, and
> there is probably a way around this, is that my program reads a text file.
> Eclipse did not include the text file, it only included the the binary
> files. I just copied the text file over into the same folder as the JAR,
> then everything worked perfectly! Eclipse is really a great tool.


Glad to hear that functionality has been added and that it worked for you.

Again, do make sure that the licensing terms of those jar files permit this
kind of usage.
Re: Program runs, but cannot export to JAR [message #251757 is a reply to message #250572] Sat, 01 March 2008 11:25 Go to previous message
Eclipse UserFriend
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
Previous Topic:Java remote debug - can't step in
Next Topic:Capturing Method Signature Changes?
Goto Forum:
  


Current Time: Tue Apr 22 11:37:29 EDT 2025

Powered by FUDForum. Page generated in 0.04797 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top