Java ClassLoader - Casting methods in external jars - java

Im trying to create a plugin system for one of my java project so I was trying to do it with classloader but when I try my method it gives me a ClassNotFound exception. I just can't get it working could someone help me with this? Sorry for my bad english.
my method:
public void loadPlugin(String jarname) throws Exception {
File f = new File("server'\\" + jarname + ".jar");
URL url = f.toURL();
URL[] urls = new URL[]{url};
URLClassLoader child = new URLClassLoader (urls, this.getClass().getClassLoader());
Class classToLoad = Class.forName ("me.daansander.plugin.Plugin", true, child);
Method method = classToLoad.getDeclaredMethod ("onEnable");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);
StackTrace:
java.lang.ClassNotFoundException: me.daansander.plugin.Plugin
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at me.daansander.serverchecker.plugin.ServerPlugin.loadPlugin(ServerPlug
in.java:59)
at me.daansander.serverchecker.plugin.ServerPlugin.start(ServerPlugin.ja
va:40)
at me.daansander.serverchecker.ServerChecker.<init>(ServerChecker.java:3
9)
at me.daansander.serverchecker.ServerChecker.main(ServerChecker.java:124
)

For Class.forName() to work you'd have to register your new classloader via Thread.currentThread().setContextClassLoader(myClassLoader).
See also https://stackoverflow.com/a/4096399/1015327.
Alternatively, you could try myClassloader.findClass().

Related

IOException Loading Data into BlazegraphEmbedded

I'm having an issue loading my Blazegraph properties file into an embedded instance. When I try to import my .properties file into my Java class, I get the following error:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at blazegraph_tinkerpop_tryout.blazegraph_data_load.loadProperties(blazegraph_data_load.java:55)
at blazegraph_tinkerpop_tryout.blazegraph_data_load.main(blazegraph_data_load.java:32)
Call to loadProperties function from main:
Properties props = loadProperties("sampleprops.properties");
My loadProperties function (checking to see whether file path is valid, then sending to reader):
public static Properties loadProperties(String resource) throws IOException
{
Properties p = new Properties();
Path path = Paths.get(resource);
Boolean bool = Files.exists(path);
if (bool)
{
System.out.println("File was found. Attempting data load...");
InputStream is = blazegraph_data_load.class.getResourceAsStream(resource);
p.load(new InputStreamReader(new BufferedInputStream(is)));
return p;
}
System.out.println("The file you entered was not found.");
return null;
}
Here is what my file sampleprops.properties looks like:
com.bigdata.journal.AbstractJournal.bufferMode=DiskRW
com.bigdata.journal.AbstractJournal.file=blazegraph.jnl
I have been following the setup instructions from the sample Blazegraph app described here. If it makes a difference, I am using the Blazegraph/Tinkerpop3 implementation found here.
I found a workaround: I switched my getResourceAsStream method to a FileInputStream method.
The problem was with the placement of my properties file. The FileInputStream method seems more forgiving in where you place the file.

javax.tools.JavaCompiler Setting up classpath

Im trying to write scripting system for a Minecraft Spigot plugin and im not quite sure on how to give my scripts the information from my main jar file.
With this current code it will compile the script fine but give me an error on line "3" of the Compass.java file (The class declaration). From what I see the program doesnt know what "com.deft.core.scripts.DeftScript" is and im not quite sure on how to tell it this information.
I got my compiling code from this thread if its relevant
Compiling the Script:
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(script));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit);
if (task.call()) {
Object obj = null;
try {
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./plugins/Deft-Core/").toURI().toURL()});
Class<?> loadedClass;
loadedClass = classLoader.loadClass("scripts.Compass");
obj = loadedClass.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (obj instanceof DeftScript) {
DeftScript deftScript = (DeftScript)obj;
deftScript.onEnable(this);
}
} else {
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.format("Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri());
}
}
Compass.java
package scripts;
public class Compass extends com.deft.core.scripts.DeftScript {
#Override
public void onEnabled() {
}
}
DeftScript.java
package com.deft.core.scripts;
public abstract class DeftScript {
public abstract void onEnable();
}
With com.deft.core.scripts.DeftScript being the source im trying to extend from the main jarfile.
Im pretty sure my problem lies from somewhere within here:
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");
EDIT:
Fixed my problem with setting up the class path. My jar file in question was location in another location from what I specified.
I switch out the optionList.add calls with one
optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/Deft-Core.jar"));
All tho this fixed my initial question now it as started causing a new error.
Apon trying to run my newly compiled class file it is giving me an error
java.lang.NoClassDefFoundError: com/deft/core/scripts/DeftScript
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_45]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_45]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_45]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
at com.deft.core.main.DeftCore.onEnable(DeftCore.java:79) ~[?:?]

Exception java.lang.NoClassDefFoundError android/app/Activity

In an android app, having:
public class Pars extends Activity{
public Document docout(){
InputStream is = getResources().openRawResource(R.raw.talechap01);
Document docout = null;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = domFactory.newDocumentBuilder();
docout = builder.parse(is);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return docout;
}
and
public class Manager {
public static String getStory(String spage, Document docs) {
XPathExpression expr;
XPath xpath = XPathFactory.newInstance().newXPath();
Object result = null;
String num = spage;
String par = null;
try {
expr = xpath.compile("//decision"+num+"/p//text()");
result = expr.evaluate(docs, XPathConstants.NODESET);
}
catch (XPathExpressionException e) {
e.printStackTrace();
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
par = nodes.item(i).getNodeValue();
System.out.println(par);
}
return par;
}
}
Now I want to test the code.
public class Test {
public static void main(String[] args) {
try {Pars p = new Pars();
Document doc = p.docout();
System.out.println(Manager.getStory("000", doc));
}catch (Exception e){
e.printStackTrace();
}
}
}
I run "Test" as a java application and I get:
Exception in thread "main" java.lang.NoClassDefFoundError:
android/app/Activity at java.lang.ClassLoader.defineClass1(Native
Method) at java.lang.ClassLoader.defineClass(Unknown Source) at
java.security.SecureClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.access$100(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
com.stack.over.Test.main(Test.java:11) Caused by:
java.lang.ClassNotFoundException: android.app.Activity at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) ... 13 more
I tried to find how to fix it but cannot achieve it.
I have this answer from another question but I'm kind of noob yet and can't figure it out how to fix it.
You have defined your Pars class as extending the class Activity. The
error you are seeing is telling you that the class
android.app.Activity is not available on the classpath. Maybe you need
to check which libraries you import. – adamretter Mar 5 at 10:01
Thanks all.
The error is that you're trying to run a program that references Android classes (android.app.Activity) as a regular Java program that doesn't have Android runtime libraries in its classpath.
You should run Android apps as Android apps (on an emulator or a device) and regular Java programs as regular Java programs and not mix the two. Activity is Android; main() method is regular Java.
To learn Android app development, you can start with the developer.android.com getting started documentation.
The error message says it all. You are writing Android now. Trying to write a main method doesn't make any more sense in Android than it would if you were writing a webapp that runs in Tomcat. You are going to need to step back and read up on how Android works.

java.lang.ClassNotFoundException

I've installed MaxDB in my local machine and I'm trying to make a connection to it using Java.
And I'm getting this error when running :
Exception in thread "main"
java.lang.ClassNotFoundException:
com.sap.dbtech.jdbc.DriverSapDB
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sap.maxdb.Hello.main(Hello.java:15)
This is the code I'm using :
package sap.maxdb;
import java.sql.*;
public class Hello
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
String username = "DBM";
String password = "azerty";
String dbname = "SAPDB";
Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");
String url = "jdbc:sapdb://" + dbname;
Connection connection = DriverManager.getConnection (url, username, password);
Statement stmt = connection.createStatement ();
ResultSet resultSet = stmt.executeQuery ("SELECT * FROM HOTEL.CUSTOMER");
resultSet.next ();
String hello = resultSet.getString (1);
System.out.println (hello);
resultSet.close ();
stmt.close();
connection.close ();
}
}
I did like they said in their website :
set CLASSPATH=%CLASSPATH%;C:\Program
Files\sdb\programs\runtime\jar\sapdbc.jar
But I get always the same error.
I know that I'm missing something but can't find it °!°
Waiting for your help.
Thanks.
What does "install" mean? Basically, the db server must be installed and running.
Step two is to add the driver (which you can find under C:\Program Files\sdb\programs\runtime\jar\sapdbc.jar, assuming this applies to your installation too) to the build path of your project. If the driver class cannot be found, it'll raise a ClassNotFoundException as you experienced.
You don't mention which IDE you are using, but try to set your project buildpath from the context menu.
Try adding the following code to make sure that your application picks up the CLASSPATH that you specified:
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
Copy the driver's jar file to the 'lib' folder of the server. Then restart the server.

Classloading a plugin for a custom application produces a NoClassDefFoundError

I'm having some issues wrapping my head around the concept of classloading, I've been programming for a little while now but I'm relatively knew to how classloading works, I've gone through a couple of examples and read about the details behind classloading and classes themselves, while I understand it to a certain extent theres a concept thats escaping me and seems rather hard to put into search-friendly terms.
Essentially, I'm trying to create 'guilds' for a gamemode I've been developing for Minecraft, these guilds lie in their own classes and are loaded up with the game at startup or whenever the method 'reloadGuildFiles()' is issued. I develop these classes by first exporting the main application and adding it to the classpath of the guild being created as well as the main applications dependencies.
Here is the 'reloadGuildFiles' method.
public void reloadGuildFiles() {
unloadGuildFiles();
synchronized ( _sync ) {
System.out.println( "Loading guild class files." );
File guildDataSourceDirectory = new File( "Prospect/Guilds/" );
URLClassLoader urlcl = null;
try {
urlcl = URLClassLoader.newInstance( new URL[] { guildDataSourceDirectory.toURI().toURL() }, Thread.currentThread().getContextClassLoader() );
} catch ( Exception e ) {
e.printStackTrace();
return;
}
if ( urlcl == null )
return;
for ( File guildDataFile : guildDataSourceDirectory.listFiles() ) {
if ( !guildDataFile.getName().endsWith( ".class" ) ) {
System.out.println( "Skipping " + guildDataFile.getName() );
continue;
}
try {
String className = guildDataFile.getName().substring( 0, guildDataFile.getName().lastIndexOf( "." ) );
System.out.println( "Loading: " + className + "\n" +
"\tfrom: " + guildDataFile.getPath() );
Class<?> clazz = urlcl.loadClass( className );
Object object = clazz.newInstance();
if ( object instanceof Guild == false ) {
System.out.println( "Object loaded is not an instance of Guild." );
continue;
}
Guild guild = ( Guild ) object;
if ( _guildMap.containsKey( guild.getName() ) ) {
System.out.println( "Duplicate guild names in guild map: " + guild.getName() );
continue;
}
_guildMap.put( guild.getName(), guild );
guild.onGuildLoaded();
} catch ( Exception e ) {
System.out.println( e.getMessage() );
e.printStackTrace();
continue;
}
}
}
}
}
Here is the Guild class contained within the main application.
public abstract class Guild {
public abstract String getName();
public void onGuildLoaded() {
System.out.println( "Loaded: " + getName() );
}
}
Here is the class I am trying to classload
public class Warrior extends Guild {
public String getName() {
returns "Warrior";
}
}
Here is the error it is giving me:
java.lang.NoClassDefFoundError: Guild
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at GuildManager.reloadGuildFiles(GuildManager.java:53)
at Prospect.enable(Prospect.java:64)
at PluginLoader.load(PluginLoader.java:205)
at PluginLoader.reloadPlugin(PluginLoader.java:189)
at je.d(je.java:1196)
at je.a(je.java:430)
at bg.a(SourceFile:24)
at bh.a(SourceFile:218)
at je.a(je.java:56)
at dp.a(SourceFile:85)
at net.minecraft.server.MinecraftServer.h(SourceFile:267)
at net.minecraft.server.MinecraftServer.run(SourceFile:208)
at bw.run(SourceFile:482)
Caused by: java.lang.ClassNotFoundException: Guild
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
From what I can gather and from what I understand, even though the main application is on the build path of the class to be loaded, the classloader does not recognize the class Guild. I think I need to try and make the classloader recognize the Guild class contained in the main application, is there anyway to do this or is there something I am clearly doing wrong?
The only possible problem I can see here is that Thread.currentThread().getContextClassLoader() for some reason produces a classloader that can't be used to access Guild class.
Try this instead:
urlcl = URLClassLoader.newInstance( new URL[] { guildDataSourceDirectory.toURI().toURL() }, Guild.class.getClassLoader());

Categories

Resources