Packaging and accessing resources in a JAR

I'm working on an application and I would like to package my resources (icons, about dialog images, splash screen images, release version text, etc.) in the jar file I'm going to distribute for deployment.  I would like to access these resources from the JAR file in my deployed code.  But I would also like them accessible when I'm running the code in my Eclipse IDE.  Is there a way to do this using only one code base?
My Eclipse project structure is src (folder) which contains my source code, bin (folder) which contains my class files and res (folder) which contains my resource files.
I am using the javapackager utility to create my deployment JAR and build a self-contained deployment .exe for deploying to Windows.
Is there a way to have the javapackager build a single JAR file from multiple sources (i.e. my bin and res folders)?  What do I have to do in my code so that the same code can be used to load resources when I'm running in Eclipse and the self-contained deployment?
Thanks,
Mike

The Java Tutorials has several trails for loading resources.
Retrieving Resources (The Java™ Tutorials >                    Deployment > Java Web Start)
Use the getResource method to read resources from a JAR file. For example, the following code retrieves images from a JAR file.
// Get current classloader
ClassLoader cl = this.getClass().getClassLoader();
// Create icons
Icon saveIcon  = new ImageIcon(cl.getResource("images/save.gif"));
Icon cutIcon   = new ImageIcon(cl.getResource("images/cut.gif"));
The example assumes that the following entries exist in the application's JAR file:
•images/save.gif
•images/cut.gif
See that last sentence?
You put what you need in the jar file and then use the 'getResource' methods to load them.

Similar Messages

  • Creation of developement class,package and access key

    COULD ANYBODY EXPLAIN about
    creation of developement class,package and access key
    and who will create them?

    Working With Development Objects
    Any component of an application program that is stored as a separate unit in the R/3 Repository is called a development object or a Repository Object. In the SAP System, all development objects that logically belong together are assigned to the same development class.
    Object Lists
    In the Object Navigator, development objects are displayed in object lists, which contain all of the elements in a development class, a program, global class, or function group.
    Object lists show not only a hierarchical overview of the development objects in a category, but also tell you how the objects are related to each other. The Object Navigator displays object lists as a tree.
    The topmost node of an object list is the development class. From here, you can navigate right down to the lowest hierarchical level of objects. If you select an object from the tree structure that itself describes an object list, the system displays just the new object list.
    For example:
    Selecting an Object List in the Object Navigator
    To select development objects, you use a selection list in the Object Navigator. This contains the following categories:
    Category
    Meaning
    Application hierarchy
    A list of all of the development classes in the SAP System. This list is arranged hierarchically by application components, component codes, and the development classes belonging to them
    Development class
    A list of all of the objects in the development class
    Program
    A list of all of the components in an ABAP program
    Function group
    A list of all of the function modules and their components that are defined within a function group
    Class
    A list of all of the components of a global class. It also lists the superclasses of the class, and all of the inherited and redefined methods of the current class.
    Internet service
    A list of all of the componentse of an Internet service:
    Service description, themes, language resources, HTML templates and MIME objects.
    When you choose an Internet service from the tree display, the Web Application Builder is started.
    See also Integrating Internet Services.
    Local objects
    A list of all of the local private objects of a user.
    Objects in this list belong to development class $TMP and are not transported. You can display both your own local private objects and those of other users. Local objects are used mostly for testing. If you want to transport a local object, you must assign it to another development class. For further information, refer to Changing Development Classes
    http://help.sap.com/saphelp_46c/helpdata/en/d1/80194b454211d189710000e8322d00/content.htm
    Creating the Main Package
    Use
    The main package is primarily a container for development objects that belong together, in that they share the same system, transport layer, and customer delivery status. However, you must store development objects in sub-packages, not in the main package itself.
    Several main packages can be grouped together to form a structure package.
    Prerequisites
    You have the authorization for the activity L0 (All Functions) using the S_DEVELOP authorization object.
    Procedure
    You create each normal package in a similar procedure to the one described below. It can then be included as a sub-package in a main package.
    To create a main package:
    1.       Open the Package Builder initial screen (SE21 or SPACKAGE).
    2.       In the Package field, enter a name for the package that complies with the tool’s Naming Conventions
    Within SAP itself, the name must begin with a letter from A to S, or from U to X.
    3.       Choose Create.
    The system displays the Create Package dialog box.
    4.       Enter the following package attributes:
    Short Text
    Application Component
    From the component hierarchy of the SAP system, choose the abbreviation for the application component to which you want to assign the new package.
    Software component
    Select an entry. The software component describes a set of development objects that can only be delivered in a single unit. You should assign all the sub-packages of the main package to this software component.
    Exception: Sub-packages that will not be delivered to customers must be assigned to the HOMEsoftware component.
    Main Package
    This checkbox appears only if you have the appropriate authorization (see Prerequisites).
    To indicate that the package is a main package, check this box.
    5.       Choose  Save.
    6.       In the dialog box that appears, assign a transport request.
    Result
    The Change package screen displays the attributes of the new package. To display the object list for the package in the Object Navigator as well, choose  from the button bar.
    You have created your main package and can now define a structure within it. Generally, you will continue by adding sub-packages to the main package. They themselves will contain the package elements you have assigned.
    See also
    Adding Sub-Packages to the Main Package
    http://help.sap.com/saphelp_nw04/helpdata/en/ea/c05d8cf01011d3964000a0c94260a5/content.htm
    access key used for change standard program.
    www.sap.service.com

  • Accessing resource files inside Jar using Fat Jar Eclipse plugin

    Hi,
    I want to develop single JAR file that uses a set of other JAR libraries. I am using Fat Jar Eclipse plugin. It works fine. However, I have problem to access resources files (i.e. rdf file) using relative path from my classes. Is there any idea how can I pack, set classpath and use relative path in order to make it works.
    Thanks
    Zoran

    I have solved this problem. The problem was in a way how I have trying to access it. It should be like:
    URL fileURL = this.getClass().getResource("resource/myFile.rdf");
    Zoran

  • Load Jar and access a class in jar at run time

    I need help from you.
    How to load a Jar and access a class in the jar at run time?
    When i try the following code it works fine while running in Java (Jdk1.5).If iam running the same code in servlet,ClassCastException occurs.
    Error Message : ClassCastExcption : jartest1 cannot be cast to Thing
    test.jar contains jartest.class and Thing.class
    jartest1.java
    try{
    File file =new File("test.jar");
    String lcStr ="jartest";
    URL jfile = new URL("jar", "", "file:" + file.getAbsolutePath() +"!/");
    URLClassLoader cl = URLClassLoader.newInstance(new URL[] { jfile });
    Class loadedClass = cl.loadClass(lcStr);
    Thing t=(Thing)loadedClass.newInstance();
    t.execute();
    catch(Exception e)
    System.err.println(e);
    Thing.java
    public interface Thing
    void execute();
    jartest.java
    public class jartest implements Thing
    public void exceute()
    System.out.println("Welcome");
    Thanks and Regards
    V.Senthil Kumar
    Edited by: senthilv_sun on Dec 16, 2008 8:30 PM

    senthilv_sun wrote:
    I need help from you.
    How to load a Jar and access a class in the jar at run time?
    When i try the following code it works fine while running in Java (Jdk1.5).If iam running the same code in servlet,ClassCastException occurs.
    Error Message : ClassCastExcption : jartest1 cannot be cast to ThingPresumably we can only hope that that is a transciption error. It always helps to use copy and past actual errors and code rather than manually typing them.
    test.jar contains jartest.class and Thing.classWrong.
    The interface class and plugable class must not be in the same jar.
    A plugable interface requires two components
    - Interface (generic sense)
    - Functional components.
    The Interface must be independant (own jar) so that it is available to the framework (user of plugin) and to the functional components. And the plugable component must not be on the java class path.
    This also means that we know for certain that the plugable component is also on the system class path. That is a bad idea as well.
    Given that it is pretty pointless to even speculate as to why this error is showing up. Create the correct jar layout. Test using the command line. Then test using servlets. Insure that the plugable jar is NOT on the java classpath for both tests.

  • Understanding the combination of inheritance, packages and access modifiers

    I am working on a problem to help my practical understanding of accessing inherited members and methods from a different package using both inheritance and instance variable.
    Unfortunately, I am totally lost when I tried to set this up and understand what was happening.
    Problem:
    Create a class named "classA" under package "pack1" with 4 members (int pub_a, int priv_a, int prot_a, int def_a) and methods (pub_func1, priv_func1, prot_func1, def_func1). Mark the members and methods with 4 different access specifiers (public, private, protected, and default)
    Create a class named "classB" under package "pack2", ClassB will inherit classA of package pack1. ClassB will have 2 members (int pub_b, int priv_b) and methods (pub_func2, prot_func2) with 2 different access specifiers (public, protected)
    Try accessing the members of class classA in package pack1 inside class classB of package pack2. Find out what members and methods are accessible and not-accessible through inheritance.
    Create an instance of class classA inside class classB of package pack2. Find out what members and methods are accessible and not-accessible through the instance.
    Note: In your solution, comment out the lines that are in-accessible with the actual error message at the top of the comment.
    So I produced the following code (I don't think i set this up right):
    //filename A.java
    package pack1;
    public class A {
         public int pub_a;
         private int priv_a;
         protected int prot_a;
         int def_a;
         public int pub_func1() {return 1;}
         private int priv_func1() {return 2;}
         protected int prot_func1() {return 3;}
         int def_func1() {return 4;}
    //filename B.java
    package pack2;
    import pack1.A;
    public class B extends A{
         public     int pub_b;
         private int priv_b;
         public int pub_func2() { return 5; }
         private int priv_func2() { return 6; }
         //directly inherited fields, at least one should be accessible?
         pub_a = 1;
         priv_a = 1;
         prot_a = 1;
         def_a = 1;
         int i;
         // directly inherited methods, at least one should be accessible?
         i = pub_func1();
         i = priv_func2();
         A a = new A();
         //not sure what's accessible here, as no error red underlines appear
         a.pub_a = 1;
         a.priv_a = 1;
         a.prot_a = 1;
         a.def_a = 1;     
    }I would be interested to know how one would approach and address this problem.

    //filename B.java
    package pack2;
    import pack1.A;
    public class B extends A{
         public     int pub_b;
         private int priv_b;
         public int pub_func2() { return 5; }
         private int priv_func2() { return 6; }
         //directly inherited fields, at least one should be accessible?
         pub_a = 1;//true
         priv_a = 1;// false
         prot_a = 1;//true
         def_a = 1;//false
         int i;
         // directly inherited methods, at least one should be accessible?
         i = pub_func1();//true
         i = priv_func2();//false
         A a = new A();
         //not sure what's accessible here, as no error red underlines appear
         a.pub_a = 1;//true
         a.priv_a = 1;//false
         a.prot_a = 1//false;
         a.def_a = 1;//false
    }Edited by: fun_with_me on May 31, 2008 8:30 AM

  • Accessing resources in a jar

    im working on a msn clone in jbuilder 9(enternprise), and im having a really hard time organizing my resources. i have a few icons and i grouped them into a folder name 'icons'. at first, the images can only be loaded when i place them in the class directory, which is fine until i compiled the project into a executable jar. the jar has trouble loading the images, i opened up the jar with winzip and found all the images are included. i also played around with the source for a while but with no luck. im just wondering if this have ever happened to you and how u dealt with it, thank you

    package gui;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.ImageIcon;
    import java.awt.Component;
    import javax.swing.tree.DefaultMutableTreeNode;
    import dataobjects.UserStruc;
    import javax.swing.JTree;
    import java.net.URL;
    class IconBuilder
    extends DefaultTreeCellRenderer {
    ImageIcon online;
    ImageIcon offline;
    ImageIcon away;
    ImageIcon phone;
    ImageIcon lunch;
    ImageIcon busy;
    ImageIcon brb;
    ImageIcon idle;
    URL onlineURL;
    public IconBuilder() {
    System.out.println("Creating icons");
    onlineURL = gui.IconBuilder.class.getResource("images/online.jpg");//this worked in jbuilder but not in the jar
    online = new ImageIcon(onlineURL);
    //online = new ImageIcon("images/online.jpg");
    offline = new ImageIcon("images/offline.jpg");
    away = new ImageIcon("images/away.jpg");
    phone = new ImageIcon("images/busy.jpg");
    lunch = new ImageIcon("images/away.jpg");
    busy = new ImageIcon("images/busy.jpg");
    brb = new ImageIcon("images/away.jpg");
    idle = new ImageIcon("images/away.jpg");
    public Component getTreeCellRendererComponent(
    JTree tree,
    Object value,
    boolean sel,
    boolean expanded,
    boolean leaf,
    int row,
    boolean hasFocus) {
    super.getTreeCellRendererComponent(
    tree, value, sel,
    expanded, leaf, row,
    hasFocus);
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    UserStruc user = (UserStruc) (node.getUserObject());
    if (user.getStatus().compareTo("NLN") == 0) {
    setIcon(online);
    setToolTipText(user.getUsername() + " (Online)");
    else if (user.getStatus().compareTo("FLN") == 0) {
    setIcon(offline);
    setToolTipText(user.getUsername() + " (Offline)");
    else if (user.getStatus().compareTo("BRB") == 0) {
    setIcon(brb);
    setToolTipText(user.getUsername() + " (BeRightBack)");
    else if (user.getStatus().compareTo("AWY") == 0) {
    setIcon(away);
    setToolTipText(user.getUsername() + " (Away)");
    else if (user.getStatus().compareTo("LUN") == 0) {
    setIcon(lunch);
    setToolTipText(user.getUsername() + " (Out To Lunch)");
    else if (user.getStatus().compareTo("PHN") == 0) {
    setIcon(phone);
    setToolTipText(user.getUsername() + " (On the Phone)");
    else if (user.getStatus().compareTo("BSY") == 0) {
    setIcon(busy);
    setToolTipText(user.getUsername() + " (Busy)");
    else if (user.getStatus().compareTo("IDL") == 0) {
    setIcon(idle);
    setToolTipText(user.getUsername() + " (Idle)");
    return this;

  • Why no warnning as INVISIBLE  in  package and access control ?

    Hi all.
    I have made sample source:
    ---------- Stock.java----------
    package stock;
    public class Stock {
    private int x=1;
    int y=2;
    protected int z=3;
    ---------- PrintStock.java ----------
    package printstock;
    public class PrintStock extends stock.Stock{
    public static void main(String[] args) {
    PrintStock ps = new PrintStock();
    ps.pl();
    public void pl(){
    // System.out.println(x);
    // System.out.println(y);
    System.out.println(z);
    -----------------SampleC.java----------------------
    package stock;
    public class SampleC {
    public static void main(String[] args) {
    printstock.PrintStock ps = new printstock.PrintStock();
    Stock s = (Stock) ps;
    System.out.println(s.y); // *1
    System.out.println(ps.y); // *2
    When I use the eclips, *2 gets warning INVISIBLE.
    But when I use the JDeveloper 11.1.2.1.0,
    *2 gets no warning.
    And when I run the sources on JDeveloer 11.1.2.1.0,
    I get Error INVISIBLE.
    This is a bug of JDeveloper 11.1.2.1.0 ???
    Or is there an option parameter for warning INVISIBLE ?
    Regards,
    Moto

    Hi Frank!
    Please check the behave on Eclips.
    You might get the warning before run.
    But you have no warning the behave on JDeveloper.
    You might get the Error in running.
    Thanks
    Mototaker

  • How can i  create and access resource file(with .res ext) in j2me

    hi,
    i am developing an mobile international application using jsr-238 an optional api to implement the idea of internationalizationand localization.
    please tell me how can i create .res files in my application to store strings of different languages.thanks inadvance.

    Hello GG RA,
    which fileformat do you want to use (binary or ASCII)?
    There are some LabVIEW shipping examples for File I/O
    You could use the LabVIEW NI Example Finder (Help --> Find Examples..(search item "file")) to get an overview about the different file-formats in LabVIEW.
    Hope this helped out and feel free to reply if you have more questions about File I/O in LabVIEW
    Best regards
    Benjamin

  • Problem in package and accessing classes in the same directory

    i have a class JApplet1 which calls other classes from the same directory.ALL the other classes have a main within them whereas JApplet1 has an init method within it...the problem is this JApplet1 class is not able to call the other classes. there r no compile time errors but at runtime the classes are not being called...what should i do....suggestions plz..

    What exception are you getting?

  • Project Online - offline project plan prompts for date range to be entered when accessing Resource Usage view

    When I save a project plan to my local computer from Project Online and open it up in an offline mode and access Resource Usage view I get prompted with Date Range dialog boxes with the first one saying "Show resources/assignments that start or finish
    after:" and the second one with "And before:".
    I don't have any filters that are setup in Project Online that are not available in offline mode to trigger these prompts.
    Please let me know what could be causing the date range prompts. Thanks.

    Hi,
    You probably retrieve an enterprise filter into your local file. Just open the organizer from MS Project, go to the filter tab (be sure to have your project selected in the drop down menu at the top left of right of the organizer) and delete the date range
    filter.
    Hope this helps,
    Guillaume Rouyre, MBA, MCP, MCTS |

  • Packaging and listing multiple resources

    Hello,
    I want to include a directory containing a large number of wav files in an executable Jar file and access them in my program. The number of files is very large and it would be impractical, let alone inelegant, to write code to load each file individually by name, or from a text file or otherwise. In the past I have not needed to package such media files, and have managed by listing the files in a directory and then iterating through the list to load them, as follows:
    File f = new File(directoryName);
    File[] myFiles = directoryName.listFiles();
    for(int i = 0; i < myFiles.length; i++) {
    //try to load the file
    Is there any way I can do this using URL and ClassLoader, or in some other way that does not depend on the absolute location of the directory? It should also work in an Applet loaded in a browser.
    Many thanks.

    >
    I want to include a directory containing a large number of wav files in an executable Jar file and access them in my program. >Side note: it is generally a good idea to keep compressed formats separate to other media types and classes, and to specify 'no compression' on those Jars. The Zip compression format is not good for compressing such files that already use compression.
    >
    ..The number of files is very large and it would be impractical, let alone inelegant, to write code to load each file individually by name, or from a text file or otherwise. >Why? It is trivial to write code that can read the names, and Ant can produce the file at build time.
    >
    ... It should also work in an Applet loaded in a browser.>This project would be well suited to being launched using webstart. By specifying the media Jars as 'lazy' download, the app. would arrive on screen much faster, then you might use the DownloadService to download them in a background thread or 'as needed'.

  • How to create package and import from jar file?

    Hi all,
    I am writing a software and I am not sure how to create a package for the classes.
    Say I have two classes in the same directory as follows:
    testA.java
    ==========
    package AB;
    public class testA
    public static void main(String[] args){
         testB myB = new testB();
         System.out.println("A test");
    testB.java
    ===========
    package AB;
    public class testB
    public testB(){
         System.out.println("B constructor");
    both file compile without the package heading;
    both file compile using: javac -classpath .\ *.java
    Question 1:
    I cannot run testA by: java -classpath .\ testA
    I think it is a syntax error. What is the correct one?
    If I run testA by: java testA
    The only output I get is: A test
    But I am expecting: B constructor /n A test
    What went wrong?
    Question 2:
    I need to use APIs of another software. I have downloaded a .jar file (xxx.jar) with all the classes in it. And I have put "import xxx.*;" in my source file. But the compiler complains about the importing. What is the right way to copmile it?
    I have read a couple of tutorials but they don't answer my question.
    (I am using windows2000 and don't have the classpath variable.)
    Hope some one can help.
    Thanks a lot

    Try moving testA out of the package and importing 'AB.*;'
    If you have:
    ./testA.class
    ./AB/testb.class
    Then to execute testA from ./ type: java -cp . testA

  • How to use packages and jar files for applet?

    Hi!
    My applet takes a class say a.class.
    Now this class depends on other classes that are packaged
    in a directory and put separately.
    I want to put this applet on the web.
    So how do I manage the .class files in he package.
    I have tried using jar file format.But I was not able to get it working.
    Can someone please help me?
    Thanks

    Use Archive instead of codebase:
    <APPLET CODE="MyApplet.class" ARCHIVE="MyApplet.jar" WIDTH=400 HEIGHT=300></APPLET>

  • Confused about beans, packages, and jars

    Hello,
    I've created a package that contains three public classes, Animation, Animator, and AnimatedPanel. I won't explain what they do, since it's rather irrelevant; It is important to note that AnimatedPanel extends JLabel. The package is com.buchuki.animations
    What I want to do is place the AnimatedPanel onto a JPanel, in Forte. To do this, it appears that I have to make the AnimatedPanel a bean. (or do it using extra code, but that defeats the purpose of using an IDE). I understand the basics of beans, but not much more. I've searched these forums and skimmed the Java Bean tutorial, but haven't found anything helpful. (at least, not completely helpful! ;)
    The problem is, I want to make a bean out of the class com/buchuki/animations/AnimatedPanel.class, but not out of the other two classes in that package. However, I will want to use those other two classes to interact with the AnimatedPanel class in the application I am using. (I'll import com.buchuki.animations.*;)
    From what I've read, it appears that you have to run the jar command from the parent directory of the package (ie: in animations/com/buchuki/animations, I run the jar command from animations/). But that would include all files in com, would it not? I guess I don't know what to do at all, but how do I make a bean out of AnimatedPanel and yet keep the bean as a part of the package that uses the "helper" classes? Is it a semi-advanced entry in the manifest file?
    Thanks,
    Dusty

    Ok, I've figured out a few things in my research, but it's still not working. I guess that the Java-Bean: True applies to only one package, and not to the jar archive as I first thought.
    However, I'm still a little unsure how I can include the package in the jar archive as a part of my program; is it that I should use the extension mechanism mentioned in the jar tutorial?
    And at any rate, even with a manifest file like this:
    Name: com/buchuki/animations/AnimatedPanel.class
    Java-Bean: True
    and archiving with:
    jar cfm mainfest.txt animations.jar com
    This creates the archive, but when I try to add new Java Bean in forte, it says that the archive is not a valid bean. What am I missing? Did I mess up the package thing, the compilation... what?
    Thanks again,
    Dusty

  • Read access to procedures,function,packages and triggers

    Hi,
    I created a user with CREATE SESSION,SELECT ANY TABLE privilege. My objective is to create a user with read only access to other schemas. But the newly created user is not able to read procedures,function,packages and triggers. The new user need read access to procedures,function,packages and triggers. What is the priviege required for this access? Please help me to resolve this issue.
    Regards,
    Mat.

    Hi,
    Grant select all will give select privileges to all schema level objects except procedures,function,packages and triggers. But I need to grant read privileges on these objects to newly created user.
    Regards,
    Mat.

Maybe you are looking for

  • HT1320 Can i sync a different itunes account to an old Ipod that was set up with a different account that is no longer active

    I have found my old ipod that I would like to sync to a different itunes account that it was not set up, how do i sync it to my current account.  The old account information is lost and do not have it

  • How to set property for Cluster Environment for JMS Adapter

    Hi All, I am moving from DEV to Prod environment which is cluster. Can you Please explain me what property I need to Set for Cluster Environment for JMS Adapter, so that I could avoid race condition for Dequeue/enqueue. I am using soa suite 10.1..3.4

  • IChat Client Compatibility.

    I have the iChat server configured and running on my 10.5.1 (Build 9B18) server. NAT/Ports on the firewall are configured for text/audio/video. Server has the same FQDN on the outside of the firewall and inside the firewall. The problem comes up that

  • Error when i trying to start Alert Config from REB

    HI, Here is the error, 404   Not Found SAP J2EE Engine/6.40  The requested resource does not exist. Details:   Go to main page of this application!.Kindly let me know what should do i do for this. Regards, Madhu

  • Email Server Constantly Down

    "Oops! Something just went wrong on our server! We've logged the problem and will fix it as soon as possible. Sorry for the inconvenience." This problem has existed for days. I have never successfully been able to send an email message to my printer.