Java - class loading results in ClassNotFoundException for parent class - java

I am currently making a small plugin framework for a program in Java.
Basically GRPluginManager.loadPlugins() loops trough all jar files in a directory, then reads the name of the Main-class of this plugin from a specific file inside of the jar and loads it into the classpath. After that it calls the enable() method of this plugin-Main-class (which extends GRPlugin).
However, it doesn't work. It looks like the loading of the plugin-Main-class works fine and only the parent class (GRPlugin) can not be found. But it definitely exists.
GRPlugin.java
package io.github.grengine.core.plugins;
public class GRPlugin {
public void enable(){
}
public void disable(){
}
}
GRPluginManager.java
package io.github.grengine.core.plugins;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.yaml.snakeyaml.Yaml;
import io.github.grengine.JarUtils;
import io.github.grengine.Log;
import io.github.grengine.Main;
public class GRPluginManager {
private static final String PLUGIN_PATH = Main.main.getDataFolder()+File.separator+"storage"+File.separator+"plugins";
private ArrayList<GRPlugin> plugins = new ArrayList<GRPlugin>();
public ArrayList<GRPlugin> loadPlugins() throws Exception{
File filePath = new File(PLUGIN_PATH);
File files [] = filePath.listFiles();
//Iterate over files in the plugin directory
for(File file:files){
if(file.isFile()){
Log.Info("Reading "+file);
ZipFile zipFile = new ZipFile(file);
String fullyQualifiedName = null;
String plname = null;
String plversion = null;
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = (ZipEntry) entries.nextElement();
if(ze.getName().equals("gre"+File.separator+"ext.yml")){
Yaml yaml = new Yaml();
InputStream is = zipFile.getInputStream(ze);
Map<?, ?> map = (Map<?, ?>) yaml.load(is);
fullyQualifiedName = (String) map.get("main");
plname = (String) map.get("name");
plversion = (String) map.get("version");
}
}
zipFile.close();
Log.Info("Enabling "+plname+" "+plversion+" ...");
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new URL("file:"+file) });
Class<?> clazz = classLoader.loadClass(fullyQualifiedName);
GRPlugin pl = (GRPlugin) clazz.newInstance();
pl.enable();
Log.Info("Enabling "+plname+" "+plversion+" ...");
}else {
//skip folders
continue;
}
}
return plugins;
}
}
And the Main class of the plugin (the class extending GRPlugin) / Main.java
package io.github.plutos;
import io.github.grengine.Log;
import io.github.grengine.core.plugins.GRPlugin;
public class Main extends GRPlugin{
#Override
public void enable() {
Log.Info("ENABLED");
}
#Override
public void disable() {
Log.Info("DISABLED");
}
}
Here is the stacktrace:
java.lang.NoClassDefFoundError: io/github/grengine/core/plugins/GRPlugin
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_05]
at java.lang.ClassLoader.defineClass(ClassLoader.java:760) ~[?:1.8.0_05]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[?:1.8.0_05]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455) ~[?:1.8.0_05]
at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[?:1.8.0_05]
at java.net.URLClassLoader$1.run(URLClassLoader.java:367) ~[?:1.8.0_05]
at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[?:1.8.0_05]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_05]
at java.net.URLClassLoader.findClass(URLClassLoader.java:360) ~[?:1.8.0_05]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_05]
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:798) ~[?:1.8.0_05]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_05]
at io.github.grengine.core.plugins.GRPluginManager.loadPlugins(GRPluginManager.java:82) ~[?:?]
at io.github.grengine.Main.onEnable(Main.java:56) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-core.jar:git-Spigot-c5146ba-c637b93]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:356) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:316) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:418) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:382) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:337) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:256) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:528) [spigot-core.jar:git-Spigot-c5146ba-c637b93]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_05]
Caused by: java.lang.ClassNotFoundException: io.github.grengine.core.plugins.GRPlugin
at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[?:1.8.0_05]
at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[?:1.8.0_05]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_05]
at java.net.URLClassLoader.findClass(URLClassLoader.java:360) ~[?:1.8.0_05]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_05]
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:798) ~[?:1.8.0_05]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_05]
... 25 more
I just can't find a solution for this problem, I already tried different ClassLoaders (ClassLoader.getSystemClassLoader() and GRPlugin.class.getClassLoader()) ...
Can you tell what i am doing wrong? If you need more information, feel free to ask!
Thanks in advance,
fusionlightcat

If you don't set a parent classloader to your custom classloader, the system classloader will be used as parent by default.
It seems that in your environment GRPlugin is loaded by a descendant of the system classloader and hence the error.
Use this instead:
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new URL("file:"+file) }, GRPlugin.class.getClassLoader());

You can see plugin system implementation in ipscan open source project. Here is start points for exploration:
main Plugin interface. Any of plugins in ipscan must implement it.
PluginLoader class is your GRPluginManager analog. It loads plugins from different places (in project jar, external jars etc.) Look here implementation of PluginClassLoader which extends URLClassLoader and loads classes from jar files.
feeders folder with some inproject plugins which implemented Plugin interface.
UPDATE: also you can reuse for your plugin system standard java.util.ServiceLoader or some alternative solution

Related

Exception in Application Constructor while trying to use Media Player

I have written a code which will play music when a link on a webpage is found.
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javafx.application.*;
// * #author Archit
public abstract class WebCrawl extends Application{
public static void main(String[] args) throws IOException {
Application.launch(args);
int a=0;
try {
Document doc = Jsoup.connect("https://in.bookmyshow.com/ranchi").get();
org.jsoup.select.Elements links = doc.select("a");
for (Element e: links) {
if ((e.attr("abs:href").equals("https://in.bookmyshow.com/ranchi/movies/fan/ET00025074"))) {
try {
File f = new File("/Users/Archit/Documents/Music/campbell.wav");
Media hit = new Media(f.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
} catch(Exception ex) {
System.out.println("Exception");
}
}
}
}
The error I am getting is this:
Exception in Application constructor java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class webcrawl.WebCrawl
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application webcrawl.WebCrawl
A window seems to open when I run the application but is automatically closed and this error appears.
I would really appreciate the help. Thank you.
You are starting an application from the WebCrawl class using Application.launch(String[]), so launch tries to create a WebCrawl instance, which it can't, since WebCrawl is abstract.
BTW: Placing code after the Application.launch call won't work, since after Application.launch is finished, the JavaFX platform will already have exited.
You can read about the application lifecycle in the Life-cycle section of the Application javadoc.
You need to call the code form the start method or later.
You can find a tutorial for a simple JavaFX application here: https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm

com.bluelinelabs.logansquare.NoSuchMapperException

I write simple code, but it's not working.
Bean.class
package me.codeboy.lyd.test;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import com.google.gson.annotations.SerializedName;
/**
* Created by yuedong on 6/26/15.
*/
#JsonObject
public class Bean {
#JsonField
#SerializedName()
public String name ;
}
Test.class
package me.codeboy.lyd.test;
import com.bluelinelabs.logansquare.LoganSquare;
import com.google.gson.Gson;
import java.io.IOException;
/**
* Created by YD on 5/25/15.
*/
public class Test {
public static void main(String[] args) throws IOException {
test();
}
public static void test() throws IOException {
Bean bean = new Bean();
bean.name = "name";
Gson gson = new Gson();
String source = gson.toJson(bean, Bean.class);
System.out.println(source);
LoganSquare.parse(source, Bean.class);
}
}
I use Gson to generate json string , then I use LoganSquare to deserialize the json string, finally, the exception occurs. The exception is as follow:
{"name":"name"}
Exception in thread"main"com.bluelinelabs.logansquare.NoSuchMapperException:Class me.codeboy.lyd.test.Bean could not be mapped to a JSON object.Perhaps it hasn't been annotated with #JsonObject?
at com.bluelinelabs.logansquare.LoganSquare.mapperFor(LoganSquare.java:165)
at com.bluelinelabs.logansquare.LoganSquare.parse(LoganSquare.java:44)
at me.codeboy.lyd.test.Test1.test(Test1.java:26)
at me.codeboy.lyd.test.Test1.main(Test1.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by:java.lang.ClassNotFoundException:me.codeboy.lyd.test.Bean$$JsonObjectMapper
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at com.bluelinelabs.logansquare.LoganSquare.mapperFor(LoganSquare.java:161)
...8more
Process finished with exit code 1
It needs apt 'com.bluelinelabs:logansquare-compiler:1.1.0'

Class Not Found Exception When Creating New Instance Of Class Inside of Jar - Java

I am trying to create a new instance of a class to invoke a method in Java. But because the class is inside a jar (Which loads just fine), The class cannot. This causes a ClassNotFoundException to be thrown. Could someone please tell me how to fix this?
Code:
private static void loadClassFromJar(String PluginJar) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
File PluginFile = new File("./debug/plugins/DiamondCorePlugin.jar");
URL[] PluginURLs = { PluginFile.getAbsoluteFile().toURI().toURL() };
URLClassLoader ClassLoader = URLClassLoader.newInstance(PluginURLs);
Class<?> PluginClass = ClassLoader.loadClass("net.trenterprises.diamondcore.plugin.Main");
Method EventMethod = PluginClass.getMethod("onEnable");
EventMethod.invoke(PluginClass.newInstance());
}
Stack trace:
java.lang.ClassNotFoundException: net.trenterprises.diamondcore.plugin.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at net.trenterprises.diamondcore.cross.api.javaplugin.sub.Server.getClassCaller(Server.java:23)
at net.trenterprises.diamondcore.cross.api.javaplugin.sub.Server.getLogger(Server.java:15)
at net.trenterprises.diamondcore.plugin.Main.onEnable(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at net.trenterprises.diamondcore.cross.api.PluginLoader.loadClassFromJar(PluginLoader.java:55)
at net.trenterprises.diamondcore.cross.api.PluginLoader.loadAllPlugins(PluginLoader.java:25)
at net.trenterprises.diamondcore.DiamondCoreServer.<init>(DiamondCoreServer.java:47)
at net.trenterprises.diamondcore.run.main(run.java:15)
Found the problem! It turned out that the class was loading just fine, but in another class that tried to get the classes name, becuase the class was in a JAR file. It could not load! Thank you for all of the help immibiz and vanza!

Java- serializing non serializable objects using Xstream

I'm trying to serialize a third-party class (which is not serializable) and I'm using xstream to do so.
At first I tried to build some class to see if it goes well and everything worked fine, But when I tried to convert the third-party instance to xml it thorws exceptions.
the code:
import java.util.List;
import java.util.Set;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import vohmm.application.SimpleTagger3;
public class Tagger{
public static void main(String[] args) {
try {
SimpleTagger3 tagger = new SimpleTagger3(args[0]);
XStream xStream = new XStream(new DomDriver());
String tagger_xml = xStream.toXML(tagger);
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
the exceptions:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sleepycat/je/DatabaseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
at java.lang.Class.getDeclaredMethod(Class.java:2002)
at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.getMethod(SerializationMethodInvoker.java:164)
at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.getMethod(SerializationMethodInvoker.java:148)
at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.supportsReadObject(SerializationMethodInvoker.java:105)
at com.thoughtworks.xstream.converters.reflection.SerializableConverter.isSerializable(SerializableConverter.java:107)
at com.thoughtworks.xstream.converters.reflection.SerializableConverter.canConvert(SerializableConverter.java:103)
at com.thoughtworks.xstream.core.DefaultConverterLookup.lookupConverterForType(DefaultConverterLookup.java:56)
at com.thoughtworks.xstream.XStream$1.lookupConverterForType(XStream.java:498)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:48)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:250)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:226)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:189)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:135)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:83)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:43)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:82)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:1022)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:1011)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:984)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:971)
at NewDemo.main(NewDemo.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: com.sleepycat.je.DatabaseException
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 56 more
Is there a resone why xstream won't work with SimpleTagger3? Is there any other way to export java object?
Your classloader failed to load a class named 'com.sleepycat.je.DatabaseException'.
Make sure that this class is available. Try Class.forName("com.sleepycat.je.DatabaseException") to test if the class is available.
If not add it to your classpath or load it with a ClassLoader.

error while creating an xml file from jsp

I am trying to create an xml file from jsp.
This is my GenerateXml.java file. It is located in sap_workshop/WEB-INF/src/MyPackage.
package MyPackage;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.lang.*;
import java.io.Serializable;
public class GenerateXml implements Serializable{
private String wDate="a";
public void setDate(String wDate) {
this.wDate = wDate;
}
public String getDate() {
return this.wDate;
}
}
I have a workshop_html_snippet.jsp file in sap_workshop folder.
The file is:
<?xml version="1.0" encoding="UTF-8"?>
<%# page contentType="text/xml %>
<jsp:useBean id="xml" scope="page" class="sap_workshop/WEB-INF/src/MyPackage.GenerateXml"/>
<Workshop>
<Scheduled>
<WorkshopDate><% out.print(xml.getDate()); %></WorkshopDate>
</Scheduled>
</Workshop>
When i compile the java file, it doesnot show any errors but when i execute i get following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: GenerateXml (wrong name: MyPackage/GenerateXml)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Thank you.
You need to define your bean like so
<jsp:useBean id="xml" scope="page" class="MyPackage.GenerateXml"/>
and ensure that your class is in your classpath.
The stacktrace is as a result of the malformed bean name. The format is:
package.BeanName

Categories

Resources