I have to write a .jar class loader in an assignment for school. I've found this code in another question but can't figure out why it wont work.
I've set the path to be an absolute path to make sure it references to the right file.
System.out.println("Loading game: " + name);
URL url = new URL("file:///home/<name>/Documents/School/thema2.3/batavus/batavus/gamemodules/TicTacToe.jar");
ClassLoader loader = new URLClassLoader(new URL[]{url});
Class<?> clazz = loader.loadClass("spatboard.game." + name);
Class<? extends Game> boardClass = clazz.asSubclass(Game.class);
Constructor<? extends Game> ctor = boardClass.getConstructor();
return ctor.newInstance(board);
The error I get is:
Loading game: TicTacToe
java.lang.ClassNotFoundException: spatboard.game.TicTacToe
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at spatbord.loader.GameLoader.loadModule(GameLoader.java:29)
at spatbord.model.Board.<init>(Board.java:51)
at spatbord.controller.GameController.onMatchStarted(GameController.java:157)
at spatbord.connection.Server.matchStarted(Server.java:428)
at spatbord.connection.Server.handleInput(Server.java:346)
at spatbord.connection.Server.run(Server.java:145)
at java.lang.Thread.run(Thread.java:745)
The package declaration in the TicTacToe is package spatbord.game;
The contents of the jar are:
spatbord
-> game
-> TicTacToe.class
What am I not seeing here?
Look at this stacktrace:
Loading game: TicTacToe
java.lang.ClassNotFoundException: spatboard.game.TicTacToe
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at spatbord.loader.GameLoader.loadModule(GameLoader.java:29)
at spatbord.model.Board.<init>(Board.java:51)
at spatbord.controller.GameController.onMatchStarted(GameController.java:157)
at spatbord.connection.Server.matchStarted(Server.java:428)
at spatbord.connection.Server.handleInput(Server.java:346)
at spatbord.connection.Server.run(Server.java:145)
at java.lang.Thread.run(Thread.java:745)
and the contents:
spatbord
-> game
-> TicTacToe.class
Specifically at this line:
java.lang.ClassNotFoundException: spatboard.game.TicTacToe
the JRE is expecting spatbord.game.TicTacToe, so this means you mispelled your package spatboard spatbord.
Please Use URLClassLoader instead of ClassLoader in 3rd line of code.
URLClassLoader loader = new URLClassLoader(new URL[]{url});
Let me know this worked or not.
Related
I am trying to progamatically compile and load protobuf code in an eclipse plugin but for some reason it gives me this error:
java.lang.NoClassDefFoundError: com/google/protobuf/ExtensionRegistryLite
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
I know I have the dependencies set up correctly because I am able to import ExtensionRegistroyLite and use it. Also when I run this code as a Java application it works fine. Here is how I am compiling and loading the class:
compiler.run(null, null, null, "-d", "path/to/dir, "-classpath", "protobuf-java-3.11.4.jar", "myProtoClass.java")
URL url = protoDir.toUri().toURL();
URL[] urls = new URL[]{url};
// load compiled class
ClassLoader cl = new URLClassLoader(urls);
// create object of compiled class
Class<?> cls = Class.forName(fullMessageName, true, cl); // Crashes at this line
If I scroll down further in the errors it says:
Caused by: java.lang.ClassNotFoundException: com.google.protobuf.ExtensionRegistryLite
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
Any hints as to what may be causing this would be appreciated!
I am trying to inject a .jar file in a running VM.
I've added tools.jar in the build path in eclipse, but when I try to run the injector, this error pops up. How should I add tools.jar to the project?
Full error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
at src.testinjector.MainClass.main(MainClass.java:13)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
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)
... 1 more
you may try:
Give the path to tools.jar instead of being dependent on JAVA_HOME.
Note: JAVA_HOME/path to jdk/bin/ should not have any space.
By default, the "tools.jar" classes are not loaded, your options are to either use a jdk that does load it, include tools.jar in your jar, or forcefully load it with the method below, (note: this might not work on certain jdk/jvm's)
private static void prepareAttach() throws NoSuchMethodException, MalformedURLException, InvocationTargetException, IllegalAccessException {
String binPath = System.getProperty("sun.boot.library.path");
// remove jre/bin, replace with lib
String libPath = binPath.substring(0, binPath.length() - 7) + "lib";
URLClassLoader loader = (URLClassLoader) [This Class].class.getClassLoader();
Method addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addURLMethod.setAccessible(true);
File toolsJar = new File(libPath + "/tools.jar");
if (!toolsJar.exists()) throw new RuntimeException(toolsJar.getAbsolutePath() + " does not exist");
addURLMethod.invoke(loader, new File(libPath + "/tools.jar").toURI().toURL());
}
I'm trying to use MultiClassFLDA in discriminant analysis package but I always get an error on running the code and defining a new instance of the MultiClassFLDA class
Exception in thread "main" java.lang.NoClassDefFoundError: no/uib/cipr/matrix/Vector
at assignment2.face.tryLDA(face.java:141)
at assignment2.Assignment2.main(Assignment2.java:106)
Caused by: java.lang.ClassNotFoundException: no.uib.cipr.matrix.Vector
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
It seems to be linked to some dynamic class loading in newer versions of Weka, presumably within the Weka Package Manager: that class is defined in the mtj.jar, which is bundled inside weka.jar. In that other question, an answer suggested to extract the mtj.jar and add it to your classpath.
As I didn't have this problem with other Filters, my guess is that MultiClassFLDA is not implemented correctly: I found out is that if you use another Filter before, that specific class will get loaded:
// Run a dummy Filter for correct initialization
Filter f = new Standardize();
f.setInputFormat(data);
Filter.useFilter(new Instances("", params, 0), f); // Dummy run on empty dataset
// Now run the MultiClassFLDA
f = new MultiClassFLDA();
f.setInputFormat(data);
data = Filter.useFilter(data, f);
N.B. that is a really ugly hack! I used it to be able to work. I'll edit my answer when I find the appropriate way to do it (other than extracting the jar from Weka itslef).
Im using Gson for some Jayway code that Im writing:
private static final Configuration JACKSON_CONFIGURATION = Configuration
.builder()
.mappingProvider(new GsonMappingProvider())
.jsonProvider(new GsonJsonProvider())
.build();
but when I run it (Trying to apply some criteria for selection of elements inside the JSON), this exception occurred:
Exception in thread "main" java.lang.NoClassDefFoundError: net/minidev/json/writer/JsonReaderI
at com.jayway.jsonpath.internal.DefaultsImpl.<init>(DefaultsImpl.java:17)
at com.jayway.jsonpath.internal.DefaultsImpl.<clinit>(DefaultsImpl.java:15)
at com.jayway.jsonpath.Configuration.getEffectiveDefaults(Configuration.java:48)
at com.jayway.jsonpath.Configuration.access$000(Configuration.java:34)
at com.jayway.jsonpath.Configuration$ConfigurationBuilder.build(Configuration.java:229)
at com.jayway.jsonpath.internal.filter.ValueNode$PathNode.evaluate(ValueNode.java:778)
at com.jayway.jsonpath.internal.filter.RelationalExpressionNode.apply(RelationalExpressionNode.java:37)
at com.jayway.jsonpath.Criteria.apply(Criteria.java:57)
at com.jayway.jsonpath.internal.path.PredicatePathToken.accept(PredicatePathToken.java:75)
at com.jayway.jsonpath.internal.path.PredicatePathToken.evaluate(PredicatePathToken.java:59)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:77)
at com.jayway.jsonpath.internal.path.PathToken.handleArrayIndex(PathToken.java:133)
at com.jayway.jsonpath.internal.path.ArrayPathToken.evaluateIndexOperation(ArrayPathToken.java:63)
at com.jayway.jsonpath.internal.path.ArrayPathToken.evaluate(ArrayPathToken.java:52)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:77)
at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:164)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:151)
at com.wfx.wte.Test.fn(Test.java:44)
at com.wfx.wte.Test.main(Test.java:32)
Caused by: java.lang.ClassNotFoundException: net.minidev.json.writer.JsonReaderI
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 25 more
It seems to be looking up some net.minidev classes. Any clues?
add net.minidev.json-smart to the classpath
if needed,add net.mimidev.asm to the classpath
I have wriitten following code using the Jena Library to print the URIs on a web page but it is showing a error. The code is
public static void test(String url)
{
try
{
System.out.println("to go");
Model read = ModelFactory.createDefaultModel().read(url);
System.out.println("to go");
StmtIterator si=read.listStatements();
System.out.println("to go");
while(si.hasNext())
{
Statement s=si.nextStatement();
Resource r=s.getSubject();
Property p=s.getPredicate();
RDFNode o=s.getObject();
System.out.println(r.getURI());
System.out.println(p.getURI());
System.out.println(o.asResource().getURI());
}
}
catch(JenaException | NoSuchElementException c)
{ }
}
Can anyone help me further with this problem???
It is not able to create a model read object. The URL is a web address of a xml page.
the following error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.hp.hpl.jena.util.Metadata.<clinit>(Metadata.java:39)
at com.hp.hpl.jena.JenaRuntime.<clinit>(JenaRuntime.java:37)
at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.<clinit>(RDFReaderFImpl.java:74)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.<clinit>(ModelCom.java:54)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:142)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:136)
at web.crawler.WebCrawler.test(WebCrawler.java:52)
at web.crawler.WebCrawler.main(WebCrawler.java:98)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
Add all the jars in the lib/ directory of the distribution. You can use the lib/* form for the classpath.
if you then still get missing org.w3c.dom.ElementTraversal (which is in xml-apis), it's because you have an older version of Xerces and old xml-apis.
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
the library slf4j*.jar is missing from your classpath.
Following are the libraries I had to add more:
import org.slf4j.*;
import org.apache.xerces.util.XMLChar;
import org.w3c.dom.ElementTraversal;
import org.apache.jena.iri.IRIFactory;
I solved it adding all the libraries availables at jena lib dir.
I gess Jena need another lib or jar file, but I dont know the especific .jar, so I add all of them.
Then it works!