aspose file tools*
The moose likes Beginning Java and the fly likes Import *.jar File in Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Import *.jar File in Java Code" Watch "Import *.jar File in Java Code" New topic
Author

Import *.jar File in Java Code

ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
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
    Marshal

    Joined: Mar 22, 2005
    Posts: 41508
        
      53
    Are you using the -classpath switch? It should work with something like


    Ping & DNS - my free Android networking tools app
    ManChun Lam
    Ranch Hand

    Joined: May 04, 2008
    Posts: 32
    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
    Marshal

    Joined: Mar 22, 2005
    Posts: 41508
        
      53
    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

    Joined: May 04, 2008
    Posts: 32
    That solves my problem. Thank you very much for your help.

    ManChun Lam
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Import *.jar File in Java Code