• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Load third party class in java

Cali3350

Supreme [H]ardness
Joined
Mar 22, 2002
Messages
4,701
Hey guys, I apologize for a (very potentially) stupid and/or annoying question but my normal googling has been failing me...

Without having to go into implementation details, please tell me if the following is possible:
Can I load a third party java class file through a simple dialog box and then call methods within that class (assuming I know the method names?)

I am teaching a local high school basic programming class (very basic). I have asked the class to implement a few basic integer/string operations (compare inputs, etc) utilizing java. They are all submitting me a .class file that has these methods (they all have the same method names per instruction). I want to just make a program that can load the class files and then call the methods. Is this possible? I know it is with .DLL's so im assuming it is with classes too.

My assumption would be open a dialog box, copy a path name as a sting, pass the string to a classloader and then simply call loadedclass.methods(). Am i totally off base?
 
So upon hours of googling it appears that the only way to do what I want is to have all of the class files they turn in be an extension of an interface. Is this true?

It seems I can load in their files with Class.forName() but all of the files that are loaded in need to be built upon a pre-determined type (an interface).

Do I have that right?
 
So upon hours of googling it appears that the only way to do what I want is to have all of the class files they turn in be an extension of an interface. Is this true?

It seems I can load in their files with Class.forName() but all of the files that are loaded in need to be built upon a pre-determined type (an interface).

Do I have that right?

Thats one way to do it, but you don't necessarily have to go that far. What you are trying to do is readily available via the Reflection API, which is part of the standard JDK.

In particular you can load the class as you stated with Class.forName(). Once you have the class you must get the constructor (any constructor you desire, and know exists), and then invoke it to create an instance of the object. Once you have the object you need to get the method you want to call, and invoke it.
 
Thats one way to do it, but you don't necessarily have to go that far. What you are trying to do is readily available via the Reflection API, which is part of the standard JDK.

In particular you can load the class as you stated with Class.forName(). Once you have the class you must get the constructor (any constructor you desire, and know exists), and then invoke it to create an instance of the object. Once you have the object you need to get the method you want to call, and invoke it.

Perfect sir. Thank you very much! :)
 
So one more potentially embarrassing question...

Is there a way to use forName() by passing a file path? In other words instead of package_name.class_name can I simply pass it "/etc/classyclass.class" and have it load it in like that? Or alternatively is there a way to use a file path to dervive the proper class name?
 
So one more potentially embarrassing question...
This is a pretty advanced topic. To many java programmers class loading is black magic, so no need to be embarrassed as it is an excellent question.

You can construct a URLClassLoader at runtime and use it to explicitly load classes that are not on the application classpath. I recommend reading the documentation for further guidance.

The files loaded by the URLClassLoader must be in a standard package hierarchy relative to the URL you pass the constructor.

Example:
com.example.MyClass is defined in the location below

/home/user/javaclasses/com/example/MyClass.class

So I would give the URLClassLoader 'file://home/user/javaclasses/'
and then ask it to load com.example.MyClass explicitly


ADDED:

After re-reading I realize I may not have given you the answer you were looking for. If your intent is to choose an arbitrary file and try to load it as a class you'll have to extend ClassLoader and override the findClass(String) method to load the file as a byte array and pass it to defineClass(String, byte[], int, int) then return the resulting Class instance to the caller.
 
Last edited:
Back
Top