• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Import *.jar File in Java Code

 
ManChun Lam
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java 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.
     
    Ulf Dittmer
    Rancher
    Posts: 42969
    73
    • 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: 42969
    73
    • 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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic