Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Adding jars at runtime  (Read 6088 times)
0 Members and 1 Guest are viewing this topic.
Offline dranonymous

Junior Devvie




Hoping to become a Java Titan someday!


« Posted 2004-11-09 14:04:14 »

Here's a class I came across while trying to solve a problem I had recently.  Its a 'hack' so to speak, but it does work well.  I have an app that uses the Comm API with java.  The issue I had was the nusance of installing the app.  You had to copy a few files to the jre and then set some classpath stuff.  Not a big deal per se, but annoying, especially when I wanted a simple drag and drop solution.

This class helped provide that, by allowing me to add the comm.jar at runtime without having to make a batch file or modify the environment variables.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
import java.io.*;
import java.net.*;
import java.lang.reflect.*;

public class ClassPathHacker {

      private static final Class[] parameters = new Class[] { URL.class };

      public static void addFile(String s) throws IOException {
            File f = new File(s);
            addFile(f);
      }
     
      public static void addFile(File f) throws IOException {
            addURL(f.toURL());
      }

      public static void addURL(URL u) throws IOException {

            URLClassLoader sysloader =
                  (URLClassLoader) ClassLoader.getSystemClassLoader();
            Class sysclass = URLClassLoader.class;

            try {
                  Method method = sysclass.getDeclaredMethod("addURL", parameters);
                  method.setAccessible(true);
                  method.invoke(sysloader, new Object[] { u });
            } catch (Throwable t) {
                  t.printStackTrace();
                  throw new IOException("Error, could not add URL to system classloader");
            }

      }
}


I don't take credit for this, I came across it at java.net on the forums.

Regards,
Dr. A>
Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #1 - Posted 2004-12-01 06:38:14 »

very handy!  Great for loading plugins and maps bundled as .jar files on the fly.

Will.

Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #2 - Posted 2004-12-02 08:25:26 »

Wheres the difference to creating a new URLClassLoader?

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

« JGO Spiffy Duke »


Medals: 212
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2004-12-02 08:51:40 »

It appears that this is acutally dropping the new URL into the system class loader which means I suppose you can pick up the new classes without having to use the new classloader anywhere else?

Kev

Offline kevglass

« JGO Spiffy Duke »


Medals: 212
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #4 - Posted 2004-12-02 09:02:14 »

Looking at it tho, a far prettier solution would probably be (just typed, no compiler handy):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public class MySystemClassLoader {
     private static MySystemClassLoader single;

     public static MySystemClassLoader get() {
          return single;
     }

     private URLClassLoader loader;

     public MySystemClassLoader(ClassLoader loader) {
           super(new URL[0],loader);
 
           single = this;
     }

     public void addURL(URL u) {
          loader.addURL(u);
     }
}

public class Hook {
  public static void main(String argv[]) {
        MySystemClassLoader.get().addURL(new URL("whatever"));
  }
}
    


EDIT: actually you'd need to set the property "java.system.class.loader" to "MySystemClassLoader" outside, i.e. java -Djava.system.class.loader=MySystemClassLoader

Reference for why I think this should work: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html#getSystemClassLoader()

This way there's no reflection involved or naughty assumptions about what the system class loader will or won't be implemented as.

Anyone tell me if this strategy would work?


Kev

Offline rritoch

Innocent Bystander





« Reply #5 - Posted 2011-06-27 10:36:46 »

Thank you for this post!

I've been fighting with getting JavaFX apps to run from a JAR and this hack works perfectly. I added a few modifications so it is easier to use for loading JAR's at runtime. My problem was that I need JavaFX, MySQL, and Java EE support, but I can't expect the end user to play with their configuration settings to get the application to run.

As for the other responses there is a HUGE difference between this method and creating a new class loader. My first attempt was to load everything via class loaders but JavaFX will not work properly when loaded from a class loader since classes built by different loaders aren't compatible.

Using this method I can search the users computer for their installation of JavaFX in the standard install folders. For the MySQL driver I include it inside the jar (jar within a jar). I save the mysql driver to the users home directory. After that loading it with the above method is trivial!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!