• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Import *.jar File in Java Code

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is a simple SWT HelloWorld code,



It works well in Eclipse, since I can set the project to reference the jar "swt.jar".

Let's say I want to deploy this, do I have to extract everything inside "swt.jar", then re-jar those files with SWTHelloWorld.java? Or can I just say something like "import swt.jar", and use everything inside it.

Thank you for all your help.
[ August 07, 2008: Message edited by: ManChun Lam ]
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a JAR file with your own application, you can specify the classpath:

This path is relative to your own JAR file, so with that example you should just put the swt.jar file in the same folder as your JAR file. You can also choose to use subfolders.

Note that you should NOT put swt.jar INSIDE your JAR file, because then you will have to use some workarounds which are, in my opinion, not worth it.
 
ManChun Lam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your point in editing the Manifest. However, my code will not even compile unless I extract everything inside swt.jar.

To better illustrate the problem:

Directory: C:\Test
Files inside Directory:
  • SWTHelloWorld.java
  • swt.jar



  • The compiler will not compile because it can't find the directory org/eclipse/swt/*, etc, even though they are inside swt.jar. Is there a way to compile SWTHelloWorld without extracting swt.jar?

    Thank you.
     
    Rancher
    Posts: 43016
    76
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you using the -classpath switch? It should work with something like
     
    ManChun Lam
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, that solves the compiling problem but when running it

    I get this error,

    If I compile and run it with swt.jar extracted, everything is fine.

    Am I missing something?

    Thank you for your help.
     
    Ulf Dittmer
    Rancher
    Posts: 43016
    76
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to specify the classpath for compilation and at runtime:

    Note that the dot is important, otherwise the SWTHelloWorld class won't get found. (This assumes that you're using Windows; if you're on Unix, use a colon instead of a semicolon.)
     
    ManChun Lam
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That solves my problem. Thank you very much for your help.

    ManChun Lam
     
    He does not suffer fools gladly. But this tiny ad does:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic