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.
Related
This question already has answers here:
can not access a member of class org.springframework.aop.TruePointcut with modifiers public
(3 answers)
Closed 10 months ago.
This is my code (I removed a lot of graphics logic as to see if it would run a most basic file)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class Client extends Application {
public static String host = "127.0.0.1";
public BufferedReader fromServer;
public PrintWriter toServer;
public Scanner consoleInput = new Scanner(System.in);
public Client() {
}
public static void main(String[] args) {
launch(args);
}
public void start(Stage displayStage) {
try {
System.out.println("HELLO");
} catch (Exception e) {
System.err.println("Client connection error!");
e.printStackTrace();
}
}
}
but the error messages are really bizarre and I'm not sure what exactly it means; it mentions a public access but afaik, public means anything can access it and I don't use reflection but its throwing errors regarding reflection and I'm really stumpted as to what could be causing this error.
Exception in Application constructor
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class eHillsClient.Client
at javafx.graphics#18/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891)
at javafx.graphics#18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalAccessException: class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) cannot access a member of class eHillsClient.Client with modifiers "public"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at javafx.graphics#18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803)
at javafx.graphics#18/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics#18/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#18/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics#18/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics#18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Make Client class public because a main class in JavaFX Applicaions must have a public access modifier to be picked by getConstructor() method in Application class.In normal cases it wouldn't cause an error but your main class will not be an entry point to the program(in JavaFx Applicaions the entry points is Applicaion class).
You can further read here
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
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
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'
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