Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[java] using extern resource jars

Started by cignox1 3 replies 500+ views
Original Post
cignox1
cignox1
Hi, I would like to put all my icons into a separate jar file, and load them at run time. I was not able to get satisfiyng informations using google, so I'm asking to you guys for a help. What I know is: -Doing this should be pretty easy. -If the pictures were in the same package of the application, then using MyApp.class.getResource("path of the resource") should be enaught. With the obtained URL I should be able to create the icon in the following way: JButton e = new JButton(command, new ImageIcon(url)); -If the pics are in a different jar, the only requirement is that this jar lies in the classpath. My question are: how can I specify the classpath at runtime? If the pics jar is in the same directory of the app jar, shouldn't it be already in the classpath? Should the previous code work in this case? I'm using NetBeans 4.0 and if I run the app I get some exceptions: java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:138) at moneyflow.ToolBarPanel.addButton(ToolBarPanel.java:45) at moneyflow.MFGui.setUI(MFGui.java:69) at moneyflow.MFGui.<init>(MFGui.java:56) at moneyflow.Login$ActionHandler.actionPerformed(Login.java:145) . . . where the first line 'at javax.swing.ImageIcon.<init>(ImageIcon.java:138)' is the instruction: JButton e = new JButton(command, new ImageIcon(io)); Thank you all for the help
Lucidquiet
Lucidquiet
Does the manifest file correctly state the class-path that is required for your app? Example:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.0
Created-By: 1.4.1_01-b01 (Sun Microsystems Inc.)
Main-Class: LispDoc.entry.Main
Class-Path: log4j-1.2.9.jar commons-logging.jar commons-cli-1.0-beta-2
-dev.jar jrythm.jar jdom.jar

Where the Class-Path: entry is a space delimitted list of all the jars the app LispDoc is dependent upon.

L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Son of Cain
Son of Cain
This methods adds all files (actually, jar files) to the classpath, at runtime. The returned URLClassLoader can be used to load classes and resources from the .jar added at runtime.

        /**     *     */    public final URLClassLoader createClassLoader(String dirPath) throws Exception {                File dir = new File(dirPath);        String[] files = dir.list(new JARFilenameFilter());                URL[] urls = new URL[ files.length ];        for (int x = 0; x < files.length; x++) {            // remember to use a backslach in the end of 'dirPath' String            File file = new File(dirPath + files[x]);             urls[x] = file.toURL();        }                return new URLClassLoader(urls);    }    /**     * FilenameFilter used for a JAR file     */    private static final class JARFilenameFilter implements FilenameFilter {        public boolean accept(File file, String str) {            return ( (!file.isDirectory()) || str.endsWith(".jar"));        }    }


Hope it helps,

Son Of Cain
a.k.a javabeats at yahoo.ca
cignox1
cignox1
Thank you both for your help. Now I load at run-time the icons as Son of Cain suggested, but it's useful to know that I should specify in the manifest all the jar's I need to load when the app start...

Thank you!
Son of Cain
Son of Cain
Lucidquiet's suggestion is best, if you know the .jars you're going to use, specify their location in the manifest file. I posted that code because you asked about how one can modify classpath at runtime.

That code is useful for plugin-enabled applications - when you know you'll have to load a "bunch of jars", but not exactly how many of them.

Son Of Cain
a.k.a javabeats at yahoo.ca

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.