Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  ClassLoader help!  (Read 1037 times)
0 Members and 1 Guest are viewing this topic.
Offline SkyAphid
« Posted 2012-08-16 12:02:18 »

Okay, so I'm making a plugin loader for my project, and this is essentially the code to it:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
File folder = new File(System.getProperty("user.dir")+"/res/modules/");
      File files[] = folder.listFiles();

         for (int a = 0; a < files.length; a++){
            String classToUse = files[a].getName().replace(".class", "");
           
            ClassLoader loader = URLClassLoader.newInstance(
                   new URL[] { folder.toURL() }, getClass().getClassLoader());
           
               Class<?> clazz = Class.forName(classToUse, true, loader);
               Class<? extends Object> starter = clazz.asSubclass(Object.class);
               Constructor<? extends Object> constructor =
                  starter.getDeclaredConstructor(String[].class, String[].class, String.class);
               Object start = constructor.newInstance(sent, tags, sentence);
         }


It passes in some necessary variables the engine itself generates for the modder to work with but there is actually one very necessary object that needs to be passed through, and that is the Brain object. This gives them access to all of the programs information that's stored in it, but I have no way of sending this seemingly.

Essentially, what I'm asking, is how would I import aida.neural.Brain into a class that is in the resources folder and not in any way connected to the rest and be able to call functions from it? I just need to it to call them blindly if so needed, just compile and do what it's supposed to once loaded.

Please also know I'm a novice with ClassLoader, so don't make fun of me too bad if this is easy, Hahah.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2012-08-16 12:15:17 »

Instead of Constructor<? extends Object> use Constructor<? extends Plugin>.

Plugin has a method, setBrain(Brain b).

Cas Smiley

Offline SkyAphid
« Reply #2 - Posted 2012-08-16 15:22:12 »

Instead of Constructor<? extends Object> use Constructor<? extends Plugin>.

Plugin has a method, setBrain(Brain b).

Cas Smiley
Alright, I'll give it a go, thank you.

Edit, it didn't work!

Here's the modified code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
File folder = new File(System.getProperty("user.dir")+"/res/modules/");
         File files[] = folder.listFiles();
   
            for (int a = 0; a < files.length; a++){
               String classToUse = files[a].getName().replace(".class", "");
               
               ClassLoader loader = URLClassLoader.newInstance(
                      new URL[] { folder.toURL() }, getClass().getClassLoader());
               
                  Class<?> clazz = Class.forName(classToUse, true, loader);
                  Class<? extends Brain> starter = clazz.asSubclass(Brain.class);
                  Constructor<? extends Brain> constructor =
                     starter.getDeclaredConstructor(String[].class, String[].class, String.class, Brain.class);
                  Object start = constructor.newInstance(sent, tags, sentence, Brain.get());
            }


Here's the test plugin code:

1  
2  
3  
4  
5  
public class ModuleTest {
   public ModuleTest(String[] sent, String[] tags, String sentence, Brain b){
      System.out.println(sentence +"/"+b.getName());
   }
}


I get the ClassCastException.

What do I do?

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-08-16 15:53:31 »

You are creating a new classloader every pass through the loop.  Every Brain class you load through this new classloader is incompatible with any Brain class loaded through any other classloader -- they're completely different classes as far as Java's concerned.

Is there a particular reason you're creating a new classloader rather than using the existing loader of whatever class uses Brain? 


Offline SkyAphid
« Reply #4 - Posted 2012-08-16 21:09:22 »

You are creating a new classloader every pass through the loop.  Every Brain class you load through this new classloader is incompatible with any Brain class loaded through any other classloader -- they're completely different classes as far as Java's concerned.

Is there a particular reason you're creating a new classloader rather than using the existing loader of whatever class uses Brain? 

Well, from my understanding, which isn't much, I couldn't load the plugin through the normal ClassLoader because that would mean it would have to be in the classpath, which is something I'd rather not force on the plugin makers. The plugins are in the resource folder.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline Danny02
« Reply #5 - Posted 2012-08-16 22:03:50 »

putting your plugins on the classpath shouldn't be that big of a problem.
You could use the classpath wildcards from Java 6:
java -cp libs/* resources/plugins/* -jar MyApp.jar
Offline SkyAphid
« Reply #6 - Posted 2012-08-16 22:23:31 »

putting your plugins on the classpath shouldn't be that big of a problem.
You could use the classpath wildcards from Java 6:
java -cp libs/* resources/plugins/* -jar MyApp.jar
It's the fact that people will have to open the Jar. Most average joes won't want to do that crap, and the little Java you'll need to know to make the plugins is all that I want to be necessary.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline Danny02
« Reply #7 - Posted 2012-08-16 22:41:12 »

why should anybody have to open any jar? and which "average" joes are you talking about^^
Offline SkyAphid
« Reply #8 - Posted 2012-08-16 22:45:38 »

why should anybody have to open any jar? and which "average" joes are you talking about^^
Well generally the ClassPath is inside a jar after compilation. I may be wrong. Average Joes being the average people using this AI's brain to make simple plugins to be more flexible for what they want it to do. If I can get the brain working outside, it'd be as a simple as this:

1  
2  
3  
4  
subject = brain.findSubject(sentence);

if (subject.related("Command here"){
do command}


Otherwise these will have to be done manually, which no one likes.

Can someone please give me some kind of Code example that would help with my problem? I'm a bit of a visual learner.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
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.

Longarmx (42 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (60 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!