Integrating dynamic classpath with Webstart?

Hey there everyone -

I’ve got a game I’m working on (the Best Game Evar 2, check my signature) that uses a dynamic classpath in order to add custom triggers to level files. Basically, each level file also has a .class file with it, and that class file is loaded in via the class loader when the level runs.

Because I’m trying to make the game available via Webstart, I need to create a resource folder where all custom user data goes. That’s no problem, but the class loader can’t find anything in the resource loader, even if I specify the path. How do I add a specific folder (System.getProperty(“user.home”) + “.bge2/LevelScripts/”) into the classpath with Webstart? I’m not sure how to put user.home in the JNLP file, either.

Thankee.

If the app is signed then you should probably use an isolated URLClassLoader to load the classes you’re talking about. Then you can add whatever you need at runtime. Also if your app is signed you can use System.getProperty(“user.home”).

Kev

Why do you need to load the class file dynamically? Can’t you put all the classes in a jar on the webserver and simply use Class.forName(“foo.bar.MyClass”) to load them?

I’m guessing these are meant to be user provided classes acting as “scripts” for some sort of modding system?

Kev

I should have been more specific, as usual. :smiley:

The game comes with a level editor. Using it, the player can type in custom Java code that is then compiled into a .class file. When the level is loaded, the class file is run constantly. The problem is that, after having created the JAR, I obviously can’t add the .class files directly into it. Instead, I need them to be saved in the user’s resource folder as a part of the new level. In order for the class loader to see it, however, it seems like the .class file needs to be in the classpath. I’m wondering how to specify that part of the resources folder as the classpath.

I have a similar problem .
I have find a solution for a non webstart application. But it seems that the class loader for webstart application is not the same and it fails.
If you find a solution, let me know ;).

URLClassLoader really is your answer. Create one, add the URL to the file system location in which your .class files exists (or to a JAR). Use that class loader to instance your classes (with loadClass()).

Kev

Hmmm it seems that i need to add the path to the jar (my web start application) because the classes are instance of one of my abstract class.
And System.getProperty(“java.class.path”) don’t return it.

Shouldn’t be neccessary. The URLClassLoader asks the parent classloader (e.g. your app) for classes it can’t load from the given jars.

I didn’t use it (i just find out myselft ;D). I work fine.


protected void loadPlugin() throws MalformedURLException
    {        
      File rep = new File(repPlugin);
      URL url = rep.toURI().toURL();
      URL[] urls = new URL[]{url};
            
      URLClassLoader loader = new URLClassLoader(urls,this.getClass().getClassLoader());
      
      plugins.removeAllElements();
      
      File fic[] = rep.listFiles();
      for(int i=0;i<fic.length;i++)
      {
        if (fic[i].getName().endsWith(".class"))
        {
          try
          {
            System.out.println("Loading class : "+fic[i].getAbsolutePath());
              
            String fileName = fic[i].getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            Class fileClass = loader.loadClass(className);
            Object fileObject = fileClass.newInstance();
            
            if (fileObject instanceof Plugin)
            {
              Plugin plugin = (Plugin)fileObject;
              plugins.add(plugin);
            }
          }
          catch(Exception ex)
          {
            System.out.println(ex.toString());
          }
        }
      }
      
      pluginListModel.setPlugins(plugins);
    }

repPlugin : String of the fullpath to the plugin folder
Plugin : my abstract class for plugins
plugins : a JList of my plugin

the plugins classes are in the default package.

Beautiful. Thanks for the help, everyone.