Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Creating Executable For SWT Program
Creating Executable For SWT Program [message #206989] Mon, 23 April 2007 21:00 Go to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
Hi. I'm trying to learn how to create an executable version of an Eclipse
Java program that uses the SWT. I've created an SWT "Hello world"
application and it runs correctly inside Eclipse.

Now, I'm trying to figure out how I would deliver this application to
another computer -- e.g., a Windows PC. In a normal Java project, I can
just export to a JAR file and then deliver the JAR file to the other
Windows PC (as long as they have the VM).

With SWT, I understand that I need to make sure the user's PC has the SWT
DLL's (there appear to be 4 of them). I've also found documentation that
indicates that the classpath must include the SWT.JAR, but I'm not sure
what this means when the JAR file is created from Eclipse. I've been
looking for instructions and haven't been able to find anything that works.

If you could point me to somewhere with instructions on how to do this, I
would be most grateful. Thanks.
Re: Creating Executable For SWT Program [message #207004 is a reply to message #206989] Mon, 23 April 2007 23:27 Go to previous messageGo to next message
Paul A. Rubin is currently offline Paul A. RubinFriend
Messages: 188
Registered: July 2009
Senior Member
Mark Dexter wrote:
> Hi. I'm trying to learn how to create an executable version of an
> Eclipse Java program that uses the SWT. I've created an SWT "Hello
> world" application and it runs correctly inside Eclipse.
> Now, I'm trying to figure out how I would deliver this application to
> another computer -- e.g., a Windows PC. In a normal Java project, I can
> just export to a JAR file and then deliver the JAR file to the other
> Windows PC (as long as they have the VM).
> With SWT, I understand that I need to make sure the user's PC has the
> SWT DLL's (there appear to be 4 of them). I've also found documentation
> that indicates that the classpath must include the SWT.JAR, but I'm not
> sure what this means when the JAR file is created from Eclipse. I've
> been looking for instructions and haven't been able to find anything
> that works.
>
> If you could point me to somewhere with instructions on how to do this,
> I would be most grateful. Thanks.
>

I found this helpful:
http://www.javalobby.org/forums/thread.jspa?threadID=15338&a mp;tstart=0.

The Eclipse wiki also has an entry:
http://eclipsewiki.editme.com/CreateStandAloneApplication.

For noncommercial use, you might want to check out
http://www.moioli.net/Projects__English____4/SWTLoader__stan dalone_SWT_application_builder___29.html
(released under GPL). I don't have any experience with it myself.

There are also ways to bundle JARs within JARs (FatJAR, for instance),
but the omniscient Eric Rizzo correctly pointed out in a previous thread
that this brings up licensing concerns.

/Paul
Re: Creating Executable For SWT Program [message #207030 is a reply to message #207004] Tue, 24 April 2007 05:10 Go to previous messageGo to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
> Mark Dexter wrote:
>> Hi. I'm trying to learn how to create an executable version of an
>> Eclipse Java program that uses the SWT. I've created an SWT "Hello
>> world" application and it runs correctly inside Eclipse.
>> Now, I'm trying to figure out how I would deliver this application to
>> another computer -- e.g., a Windows PC. In a normal Java project, I can
>> just export to a JAR file and then deliver the JAR file to the other
>> Windows PC (as long as they have the VM).
>> With SWT, I understand that I need to make sure the user's PC has the
>> SWT DLL's (there appear to be 4 of them). I've also found documentation
>> that indicates that the classpath must include the SWT.JAR, but I'm not
>> sure what this means when the JAR file is created from Eclipse. I've
>> been looking for instructions and haven't been able to find anything
>> that works.
>>
>> If you could point me to somewhere with instructions on how to do this,
>> I would be most grateful. Thanks.
>>

> I found this helpful:
> http://www.javalobby.org/forums/thread.jspa?threadID=15338&a mp;tstart=0.

> The Eclipse wiki also has an entry:
> http://eclipsewiki.editme.com/CreateStandAloneApplication.

> For noncommercial use, you might want to check out
>
http://www.moioli.net/Projects__English____4/SWTLoader__stan dalone_SWT_application_builder___29.html
> (released under GPL). I don't have any experience with it myself.

> There are also ways to bundle JARs within JARs (FatJAR, for instance),
> but the omniscient Eric Rizzo correctly pointed out in a previous thread
> that this brings up licensing concerns.

> /Paul
Paul wrote:

Thank you very much. This is exactly the information I needed.
Unfortunately, I am still doing something wrong. I've tried to run both my
HelloWorld application, using this command (note, this is with the DLL's
and JAR's copied into c:\swt):

java -classpath C:\SWT\swt.jar;C:\swt\helloworld.jar
-Djava.library.path=C:\SWT com.markdexter.swttraining.HelloWorld

I get "Exception in thread "main" java.lang.NoClassDefFoundError:
com/markdexter/swttraining/HelloWorld". The package is
com.markdexter.swttraining. I tried it with the default package and I
still couldn't get it to work. It appears that the VM can't find the
HelloWorld class, or that somehow the fact that it's in a package is
messing it up.

Then, I tried to run one of the 3.2.2 examples, adapting the directions on
the download site, with this command:

java -classpath
c:\swt\org.eclipse.swt_3.2.2.v3236b.jar;c:\swt\org.eclipse.s wt.win32.win32.x86_3.2.2.v3236jar;c:\swt\swtexamples.jar;
-Djava.library.path=c:\swt
org.eclipse.swt.examples.controlexample.ControlExample

I get pretty much the same message:

"Exception in thread "main" java.lang.NoClassDefFoundError:
org/eclipse/swt/widgets/Layout"

This time, it appears to be looking for a class "Layout" that isn't in the
swtexamples.jar or something.

I'm obviously doing something wrong or missing a step. Again, any help
will be appreciated. Thanks.
Re: Creating Executable For SWT Program [message #207126 is a reply to message #207030] Tue, 24 April 2007 19:08 Go to previous messageGo to next message
Paul A. Rubin is currently offline Paul A. RubinFriend
Messages: 188
Registered: July 2009
Senior Member
Mark Dexter wrote:

>
> Thank you very much. This is exactly the information I needed.
> Unfortunately, I am still doing something wrong. I've tried to run both
> my HelloWorld application, using this command (note, this is with the
> DLL's and JAR's copied into c:\swt):
>
> java -classpath C:\SWT\swt.jar;C:\swt\helloworld.jar
> -Djava.library.path=C:\SWT com.markdexter.swttraining.HelloWorld
>
> I get "Exception in thread "main" java.lang.NoClassDefFoundError:
> com/markdexter/swttraining/HelloWorld". The package is
> com.markdexter.swttraining. I tried it with the default package and I
> still couldn't get it to work. It appears that the VM can't find the
> HelloWorld class, or that somehow the fact that it's in a package is
> messing it up.
>
> Then, I tried to run one of the 3.2.2 examples, adapting the directions
> on the download site, with this command:
>
> java -classpath
> c:\swt\org.eclipse.swt_3.2.2.v3236b.jar;c:\swt\org.eclipse.s wt.win32.win32.x86_3.2.2.v3236jar;c:\swt\swtexamples.jar;
> -Djava.library.path=c:\swt
> org.eclipse.swt.examples.controlexample.ControlExample
>
> I get pretty much the same message:
>
> "Exception in thread "main" java.lang.NoClassDefFoundError:
> org/eclipse/swt/widgets/Layout"
>
> This time, it appears to be looking for a class "Layout" that isn't in
> the swtexamples.jar or something.
>
> I'm obviously doing something wrong or missing a step. Again, any help
> will be appreciated. Thanks.
>
>

Might have something to do with the manifest in your JAR file?

It's been a while since I used SWT, and my source code etc. is not
available to me at the moment. I can give you the following example,
for what it's worth. I have a program named ZAEdit, package also named
ZAEdit. When I deploy the program, I put zaedit.jar, swt.jar,
swt-win32-3139.dll and one other library it uses (xercesImpl.jar) all in
the same directory, then run it with

java -jar zaedit.jar

In zaedit.jar, the manifest file (manifest.mf) reads as follows:

Manifest-Version: 1.0
Main-Class: ZAEdit.ZAEdit
Class-Path: swt.jar xercesImpl.jar

If you want to put SWT in C:\SWT, you can modify the entries in the
Class-Path line accordingly.

My memory of when I generated this thing is a tad fuzzy, but as I recall
Eclipse did not give me any sort of dialog about the class path in the
manifest. I think I generated and saved the manifest file, then opened
it (in Eclipse) and added the Class-Path entry manually, then told
Eclipse to use it when writing the JAR.

HTH,
Paul
Re: Creating Executable For SWT Program [message #207355 is a reply to message #207126] Wed, 25 April 2007 05:04 Go to previous messageGo to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
Paul wrote:
> Might have something to do with the manifest in your JAR file?

> It's been a while since I used SWT, and my source code etc. is not
> available to me at the moment. I can give you the following example,
> for what it's worth. I have a program named ZAEdit, package also named
> ZAEdit. When I deploy the program, I put zaedit.jar, swt.jar,
> swt-win32-3139.dll and one other library it uses (xercesImpl.jar) all in
> the same directory, then run it with

> java -jar zaedit.jar

> In zaedit.jar, the manifest file (manifest.mf) reads as follows:

> Manifest-Version: 1.0
> Main-Class: ZAEdit.ZAEdit
> Class-Path: swt.jar xercesImpl.jar

> If you want to put SWT in C:SWT, you can modify the entries in the
> Class-Path line accordingly.

> My memory of when I generated this thing is a tad fuzzy, but as I recall
> Eclipse did not give me any sort of dialog about the class path in the
> manifest. I think I generated and saved the manifest file, then opened
> it (in Eclipse) and added the Class-Path entry manually, then told
> Eclipse to use it when writing the JAR.

> HTH,
> Paul

Mark wrote:
Thank you very much. I figured out how to make it work using the Eclipse
export wizard. It works if you create the Eclipse project as a plug-in
project as opposed to a Java project (which is what I was trying). I'm not
100% sure why, but if I create the JAR file under a plug-in project, then
I can follow the instructions you provided in your first response and it
works. (i.e., with the command "java -classpath
C:\SWT\swt.jar;c:\swt\helloworldpi.jar
-Djava.library.path=C:\SWT com.markdexter.swttraining2.HelloWorldPI",
where my package name is "com.markdexter.swttraining2", my Class name is
"HelloWorldPI", and my jar file is "helloworldpi.jar".

So I have one way to do this. I'd like to figure out the option you
discuss above using the manifest. Did you create the JAR using Eclipse or
with the jar command line? Is there a way to examine the contents of a
JAR?

Thanks again very much for your help.
Re: Creating Executable For SWT Program [message #207473 is a reply to message #207355] Wed, 25 April 2007 13:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: meerzaki.gmail.com

Hi,


If you have got the solution... plz write it clearly

so that it will be useful for other people who are trying it...

bcz.. many guys are trying to work on the same thing...


Plz plz send it clearly .........

Regards,

MR.Eclipse
Re: Creating Executable For SWT Program [message #207526 is a reply to message #207473] Wed, 25 April 2007 16:01 Go to previous messageGo to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
I'll try to write the problem and the solution as clearly as I can.

PROBLEM: Could not create a JAR file that could be executed from Windows
command line from an Eclipse Java project using SWT. These were the steps:

1. Follow the directions in the 3.2.2 Cheat Sheet called "Create a Hello
World SWT application". This instructs you to create a Java project (not a
plug-in project).
2. Finish the cheat sheet. At this point the HelloWorld application is
created and can be run correctly from within Eclipse (using Run as / SWT
Application).
3. Now, try to create a stand-alone executable for this application. The
steps for this are (a) create a helloworld.jar file using the
File/Export/Java/Jar file; (b) follow the instructions on the Eclipse
Download page for SWT Binary and Source for running a stand-alone SWT
application. In this example for Windows, create a C:\SWT directory, copy
the swt.jar (from your Eclipse project runtime directory), helloworld.jar,
and SWT JNI DLL's (e.g., for my version these are swt-win32-3236.dll,
swt-wgl-win32-3236.dll, swt-gdip-win32-3236.dll, and
swt-awt-win32-3236.dll); (d) open a CMD window and run the command (note
that my package name is "com.markdexter.swttraining"):

java -classpath C:\SWT\swt.jar;C:\swt\helloworld.jar
-Djava.library.path=C:\SWT com.markdexter.swttraining.HelloWorld

When I tried this, I got the error: Exception in thread "main"
java.lang.NoClassDefFoundError: com/markdexter/swttraining/HelloWorld

SOLUTION: If I create a new PLUG-IN project (instead of a Java project),
and follow the exact same steps ... it works! As near as I can tell, a
plug-in project adds something to the JAR file that is missing when it is
created from a Java project.

If anyone more experienced can fill in the missing piece of the puzzle, it
would be appreciated. However, this is at least a way to make this work.
It is also interesting to note that the SWT examples are available as
plug-ins (maybe for this reason?).

I hope this saves someone else some time. Thanks. Mark Dexter
Re: Creating Executable For SWT Program [message #207560 is a reply to message #207355] Wed, 25 April 2007 17:14 Go to previous messageGo to next message
Paul A. Rubin is currently offline Paul A. RubinFriend
Messages: 188
Registered: July 2009
Senior Member
Mark Dexter wrote:

> So I have one way to do this. I'd like to figure out the option you
> discuss above using the manifest. Did you create the JAR using Eclipse
> or with the jar command line?

I built the JAR inside Eclipse. Here's a detailed enumeration of what I
do for a typical project (I don't recall any differences particular to
using SWT, but then I just finished lunch and barely recall what I had).

1. With the project open, right-click the project and select Export...
then JAR file. (File -> Export should do the same thing.)

2. In the upper left pane (resources to expoert), put a check mark next
to the project and any supporting projects on which it depends (probably
none for a HelloWorld). In the right pane, I usually deselect
..classpath and .project. I'm under the impression that these are
specific to the Eclipse project, and so there's no point to exporting
them unless I'm moving the project to Eclipse on another box.

3. In the same dialog, use the Browse button to pick a location and
file name for the JAR file. I usually accept defaults on the other stuff.

4. In the next dialog, defaults should be fine. If you expect to
tinker with the program and export updated JARs, you can use the option
to save a description file in the Eclipse workspace. The description
file will show up in your workspace (assuming you put it there), and in
the future you can right-click it in the Package Explorer and tell
Eclipse to export it, which IIRC spares you repeating all the dialog steps.

5. In the next dialog, the first time out I tell it to generate a
manifest file named manifest.mf and put it in the project folder.
Thereafter, barring any changes to the manifest file, I'll tell it to
reuse the existing one. I also check the "reuse and save" box (so that
if I use the description file in the future, the manifest gets
included). I don't bother sealing things. Last, I use the Browse...
button to select the class containing the main() method. (This is
critical, and I believe mandatory.) Click Finish to generate the JAR.

6. The Package Explorer should now show manifest.mf, the JAR
description file (if you did one), and the JAR itself (if you parked the
JAR in the workspace, as opposed to elsewhere). Double-click
manifest.mf to open it for editing. It should show the main class
(Main-Class: ...). Add anything else you need (such as Class-Path: ...)
and save the file.

7. Now regenerate the JAR, using the updated manifest file. If you
saved a description file, just right click it and select Create JAR.
Otherwise, repeat the export steps above.

> Is there a way to examine the contents of
> a JAR?

Yes. It's basically a Zip archive, so you can open it with any Zip utility.

/Paul
Re: Creating Executable For SWT Program [message #207587 is a reply to message #207526] Wed, 25 April 2007 17:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: meerzaki.gmail.com

Hi Merk,

I am still getting the same kind of error ...



c:\swt>java -classpath C:\SWT\swt.jar;C:\swt\HelloWorld.jar
-Djava.library.path=C:\SWT c:\swt\HelloWorld.jar\FirstJar\Standalone
ception in thread "main" java.lang.NoClassDefFoundError:
c:\swt\HelloWorld/jar\FirstJar\Standalone

I had worked on the steps what u told..

plz help me...
Re: Creating Executable For SWT Program [message #207610 is a reply to message #207587] Wed, 25 April 2007 19:24 Go to previous messageGo to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
I don't know any more about this than I wrote in the last post. I would
just go through the steps carefully, and make sure that you are using a
Plug-in project as indicated. If that doesn't work, I don't have any
answers. Good luck. Mark
Re: Creating Executable For SWT Program [message #207626 is a reply to message #207587] Wed, 25 April 2007 20:50 Go to previous messageGo to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
I just discovered another possible problem. When I looked at the
helloworld.jar file created from the Eclipse export, it only had the
manifest.mf file and did NOT have the HelloWorld.class file. So obviously
it could not execute!! I tried a few things and could not get it to
include the .class file. Finally, I exited Eclipse and re-started it. Now
the export is working correctly and everything works, whether the project
is a plug-in or not.

So...the final answer to the problem appears to be that something happened
inside my Eclipse session so that the Export / Jar file stopped including
the HelloWorld.class file in the JAR. The take home lesson for me is this:
if you are having a problem executing a JAR, look at the JAR with WinZip
or some other ZIP utility and make sure it has the .class files you need.

Thanks. Mark Dexter
Re: Creating Executable For SWT Program [message #207642 is a reply to message #207560] Wed, 25 April 2007 21:18 Go to previous messageGo to next message
Mark Dexter is currently offline Mark DexterFriend
Messages: 324
Registered: July 2009
Senior Member
Paul Rubin wrote:

> Mark Dexter wrote:

>> So I have one way to do this. I'd like to figure out the option you
>> discuss above using the manifest. Did you create the JAR using Eclipse
>> or with the jar command line?

> I built the JAR inside Eclipse. Here's a detailed enumeration of what I
> do for a typical project (I don't recall any differences particular to
> using SWT, but then I just finished lunch and barely recall what I had).

> 1. With the project open, right-click the project and select Export...
> then JAR file. (File -> Export should do the same thing.)

> 2. In the upper left pane (resources to expoert), put a check mark next
> to the project and any supporting projects on which it depends (probably
> none for a HelloWorld). In the right pane, I usually deselect
> ..classpath and .project. I'm under the impression that these are
> specific to the Eclipse project, and so there's no point to exporting
> them unless I'm moving the project to Eclipse on another box.

> 3. In the same dialog, use the Browse button to pick a location and
> file name for the JAR file. I usually accept defaults on the other stuff.

> 4. In the next dialog, defaults should be fine. If you expect to
> tinker with the program and export updated JARs, you can use the option
> to save a description file in the Eclipse workspace. The description
> file will show up in your workspace (assuming you put it there), and in
> the future you can right-click it in the Package Explorer and tell
> Eclipse to export it, which IIRC spares you repeating all the dialog steps.

> 5. In the next dialog, the first time out I tell it to generate a
> manifest file named manifest.mf and put it in the project folder.
> Thereafter, barring any changes to the manifest file, I'll tell it to
> reuse the existing one. I also check the "reuse and save" box (so that
> if I use the description file in the future, the manifest gets
> included). I don't bother sealing things. Last, I use the Browse...
> button to select the class containing the main() method. (This is
> critical, and I believe mandatory.) Click Finish to generate the JAR.

> 6. The Package Explorer should now show manifest.mf, the JAR
> description file (if you did one), and the JAR itself (if you parked the
> JAR in the workspace, as opposed to elsewhere). Double-click
> manifest.mf to open it for editing. It should show the main class
> (Main-Class: ...). Add anything else you need (such as Class-Path: ...)
> and save the file.

> 7. Now regenerate the JAR, using the updated manifest file. If you
> saved a description file, just right click it and select Create JAR.
> Otherwise, repeat the export steps above.

>> Is there a way to examine the contents of
>> a JAR?

> Yes. It's basically a Zip archive, so you can open it with any Zip utility.

> /Paul

Mark wrote:

Thanks again Paul. I've now got it working both ways: (a) with the long
command line from the download page; and (b) using the manifest file as
you describe.

I think all of my problems were caused by two factors:

1. For some reason still unknown, I was not getting the .class file in the
JAR file. After re-starting Eclipse, this went away. I would not have been
able to figure this out without using WinZip to examine the JAR file and
see the problem.

2. Inside the manifest.mf file, the Class-Path entry must be relative to
the JAR file. So, if I put an absolute path like "C:\SWT" in the
Class-Path, the JAR would not execute with either type of command.

One minor correction to your instructions above. When you select the "Use
existing manifest from workspace" option during the export, you are not
allowed to enter in the "application entry point (i.e., main)" class. This
makes sense, since that is specified in the Main-Class entry in the mf
file.

Again, thanks for your help. Mark
Re: Creating Executable For SWT Program [message #207649 is a reply to message #207642] Wed, 25 April 2007 22:00 Go to previous message
Paul A. Rubin is currently offline Paul A. RubinFriend
Messages: 188
Registered: July 2009
Senior Member
Mark Dexter wrote:

> 1. For some reason still unknown, I was not getting the .class file in
> the JAR file.

I wonder if maybe the first time you tried to generate a JAR the package
wasn't checked (in the upper left part of the first dialog)? Then maybe
that setting got stuck somehow.

> After re-starting Eclipse, this went away. I would not
> have been able to figure this out without using WinZip to examine the
> JAR file and see the problem.

I remember the first time somebody told me you could open a JAR like a
Zip file. I had this strange feeling I was supposed to know this (but
not *why* I was supposed to know this).
>
> 2. Inside the manifest.mf file, the Class-Path entry must be relative to
> the JAR file. So, if I put an absolute path like "C:\SWT" in the
> Class-Path, the JAR would not execute with either type of command.

Didn't know that. I'm pretty sure you can override (or supplement) the
class path specified in the manifest with the -cp option in the command
line that runs the JAR, and put absolute paths there. I suppose it
makes sense; typically, when you deploy a JAR, you don't know where it's
going to be used or how their systems are structured.
>
> One minor correction to your instructions above. When you select the
> "Use existing manifest from workspace" option during the export, you are
> not allowed to enter in the "application entry point (i.e., main)"
> class. This makes sense, since that is specified in the Main-Class entry
> in the mf file.

I'm pretty sure that the first time you do this, when you don't yet have
a manifest, you can specify both "generate" and "use existing" and also
set the entry point. This is what I do (so that I can go off the
description file in the future). Thereafter, if you "use existing" then
I'm not surprised you can't change the entry point.

Glad you got it resolved.

/Paul
Previous Topic:Build with Subclipse
Next Topic:AbstractTypeMapper class
Goto Forum:
  


Current Time: Tue Apr 23 09:57:54 GMT 2024

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

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

Back to the top