Hey guys, im trying to load a class from otuside the .jar during runtime, but im being unable to.
Yes Opiop, i googled it the whole day, tried several methods, read a bunch of topics, but i get the same error.
Can anyone me guide me or give me any tip of some sort?
i have this class inside my jar
public class Script {
public void Script()
{
}
public void script(Player player, BitmapFont simpleWhiteFont) {
int playerPosX = (int) (player.getX() / 16);
int playerPosY = (int) ((player.getY() + player.getHeight() / 2) / 16); //(getY() + getHeight() / 2) / 16
}
}
I have this class below in D:\
public class test extend Script
{
public String className = "script";
public void script(Player player, BitmapFont simpleWhiteFont) {
int playerPosX = (int) (player.getX() / 16);
System.out.println("Player X Position : " + playerPosX );
}
}
I tried this below:
private void loadScript(String scriptLocation) {
// Create a File object on the root of the directory containing the class file
File file = new File("D:/");
try {
// Convert File to a URL
URL url = file.toURI().toURL(); // file:/c:/myclasses/
URL[] urls = new URL[]{url};
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// Load in the class; MyClass.class should be located in
// the directory file:/c:/myclasses/com/mycompany
Class cls = cl.loadClass("test");
script = (Script) cls.newInstance();
script.script(player, simpleWhiteFont);
} catch (Exception e) {
e.printStackTrace();
}
}
[quote]java.lang.ClassNotFoundException: test
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at br.views.levels.Level_Loader.loadScript(Level_Loader.java:70)
at br.views.levels.Level_Loader.<init>(Level_Loader.java:51)
at br.views.MenuView$3.clicked(MenuView.java:169)
at com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:85)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:345)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:303)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Java Result: -1
[/quote]