Jump to content
  • Advertisement
Sign in to follow this  
cignox1

[java] using extern resource jars

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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-

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!