I want to create ontology whit this code:
public static void main(String[] args) throws FileNotFoundException {
// TODO code application logic here
OntModel my_model= ModelFactory.createOntologyModel();
ObjectProperty op = my_model.createObjectProperty("b");
OntClass my_class = my_model.createClass("student");
DatatypeProperty dtp = my_model.createDatatypeProperty("name");
dtp.addDomain(my_class);
FileOutputStream univer= new FileOutputStream("c:/uni.owl");
my_model.write(univer,"RDF/XML-ABBREV","ns");
}
But it gives this error:
java.lang.NoClassDefFoundError: com/hp/hpl/jena/rdf/model/Resource
Caused by: java.lang.ClassNotFoundException: com.hp.hpl.jena.rdf.model.Resource
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: javaapplication6.Main. Program will exit.
Exception in thread "main" Java Result: 1
I cannot understand what is the problem.Thanks for any Help.
NoClassDefFoundError means the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
Try to download Apache Jena library from here and put it in your classpath
Related
I'm trying load an external jar file with javassist & call its main method at runtime, however when i try to do this with the below code:
File file = new File("C:\\Users\\MainPC\\Desktop\\test.jar");
ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(file.getAbsolutePath());
Class<?> MainClass = cp.get("TestPackage.MainClass").toClass();
MainClass.getMethod("main", String[].class).invoke(null, new Object[] {args});
It throws the following exception:
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:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at ReflectionTests.main(ReflectionTests.java:99)
Caused by: java.lang.NoClassDefFoundError: TestPackage/OtherClass
at TestPackage.MainClass.main(Unknown Source)
... 5 more
Caused by: java.lang.ClassNotFoundException: TestPackage.OtherClass
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 6 more
When i attempt the same thing only using the built in java reflection api it works with no problems:
File file = new File("C:\\Users\\MainPC\\Desktop\\test.jar");
URLClassLoader cl = new URLClassLoader(new URL[] {new URL("jar:file:"+file.getAbsoluteFile()+"!/")});
Class<?> clazz = cl.loadClass("TestPackage.MainClass");
clazz.getMethod("main", String[].class).invoke(null, new Object[] {args});
(the above throws no exceptions & calls the main method of the jar file as expected)
This leaves me to believe i'm doing something wrong in javassist (specifically with the loading of the jar file classes). Can someone explain to me what it is?
I should mention the jar file only contains 2 classes: MainClass.class & OtherClass.Class. Both reside in a package called TestPackage. It seems the error has something to do with the MainClass class not being able to find the OtherClass class, when javassist loads it.
The problem was that when i call ct.toClass() it only exposes the class itself to my runtimes classloader (not the entire classpool's classpath). When i then later attempt to invoke the main method of this class, my runtimes classloader tries to execute the part that loads the other class which it obviously doesn't know about and so throws a ClassNotFoundException.
The solution is to use the javassist provided classloader (javassist.Loader) which takes a classpool as argument in the constructor and then is able to load & resolve classes from the classpools classpath properly.
Here's a working code example of what i was trying to achieve:
File file = new File("C:\\Users\\MainPC\\Desktop\\test.jar");
ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(file.getAbsolutePath());
Loader loader = new Loader(cp);
Class<?> MainClass = loader.loadClass("TestPackage.MainClass");
MainClass.getMethod("main", String[].class).invoke(null, new Object[] {args});
I saw this post but I wasn't clear on what the resolution was.
I'm trying to run a java program from command line utilizing an external library. Here is the total output of my session:
grifter#host:~/java$ cat ExtractTest.java
import java.io.IOException;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
public class ExtractTest {
public static void main(String[] args) throws IOException {
System.out.println("About to open PDF");
File fh = new File("/home/grifter/test.pdf");
PDDocument d = PDDocument.load(fh);
System.out.println("Here");
PDFTextStripper ts = new PDFTextStripper();
System.out.println(ts.getText(d));
}
}
grifter#host:~/java$ javac -cp .:/home/grifter/pdfbox-1.8.5/pdfbox/target/pdfbox-1.8.5.jar ExtractTest.java
grifter#host:~/java$ java -classpath .:/home/grifter/pdfbox-1.8.5/pdfbox/target/pdfbox-1.8.5.jar ExtractTest
About to open PDF
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.pdfbox.pdfparser.BaseParser.<clinit>(BaseParser.java:68)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1219)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1187)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1158)
at ExtractTest.main(ExtractTest.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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)
... 5 more
Could someone please point me in the right direction?
Any help is appreciated.
pdf box uses dependencies, which also needs to be added to your class path. You can read about the dependecies here
The jar commons-logging and the dependencies is missing in your classpath.
You can download it here
I am trying to run my linked list in a main method, but for some reason I keep getting the following output from my terminal
Exception in thread "main" java.lang.NoClassDefFoundError: PhoneBook/java
Caused by: java.lang.ClassNotFoundException: PhoneBook.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Below I have listed the part of my class where I run the main method. Everything compiles fine, but when I run it I get that exception. Is there away I can try/catch the exception? Or is there something I am doing wrong?
import java.util.LinkedList;
public class PhoneBook<T> extends LinkedList<T>{
/**
* Creates two books, adds a person to each book, then prints out if it
* found the perons in the book.
**/
public static void main(String[] args){
PhoneBook<String> bookOne = new PhoneBook<String>();
PhoneBook<Integer> bookTwo = new PhoneBook<Integer>();
bookOne.addPerson("Obama");
System.out.print(bookOne.findPerson("Obama"));
bookTwo.addPerson(192590594);
System.out.print(bookTwo.findPerson(192590594));
}
If more information from my class is needed, I can provide it. I don't think it should be necessary because it compiled fine.
After compiling with javac PhoneBook.java are you launching it using
java PhoneBook or java PhoneBook.java ? (it should be the former not
the latter). ~Charlie
Thanks Charlie, that was it.
I wrote a simple java program
package abc.def.ghi
public class Foobar{
public String printS(String s){
System.Out.println(s);
public static void main(String [] args){
String s = args[0];
Foobar foobar = new Foobar();
foobar.printS(s);
}
Now I did javac Foobar.java
It created a class file
and then I did
java Foobar
Didnt worked
java Foobar hi //args
Didnt worked
java -cp . abc.def.ghi.Main
DIdnt worked.
Error I am getting is:
Exception in thread "main" java.lang.NoClassDefFoundError: abc/def/ghi/Main
Caused by: java.lang.ClassNotFoundException: com.intel.hadoop.graphbuilder.conf.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.intel.hadoop.graphbuilder.conf.Main. Program will exit.
Did you put your .class files in the path (path to project)/abc/def/ghi/Foobar.class?
NoClassDefFoundError: abc/def/ghi/Foobar
Looks like you didn't.
If you compile using
javac -d . Foobar.java
then the compiler will put the .class file in the right directory to match its package name, then
java abc.def.ghi.Foobar
should run it successfully.
I'm trying to run this example I found on the internet. When it gets to the following line it terminates with the following error. Error and sample-json.txt pasted below example code. Am I missing something obvious?
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
Imports:
package com.discursive.answers;
import java.io.InputStream;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
public class JsonParsing {
public static void main(String[] args) throws Exception {
InputStream is = JsonParsing.class.getResourceAsStream( "sample-json.txt");
String jsonTxt = IOUtils.toString( is );
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
double coolness = json.getDouble( "coolness" );
int altitude = json.getInt( "altitude" );
JSONObject pilot = json.getJSONObject("pilot");
String firstName = pilot.getString("firstName");
String lastName = pilot.getString("lastName");
System.out.println( "Coolness: " + coolness );
System.out.println( "Altitude: " + altitude );
System.out.println( "Pilot: " + lastName );
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
Disconnected from the target VM, address: '127.0.0.1:59138', transport: 'socket'
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
st java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at com.discursive.answers.JsonParsing.main(JsonParsing.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
Here is the sample-json.txt file that is opened.
{'foo':'bar',
'coolness':2.0,
'altitude':39000,
'pilot':{'firstName':'Buzz',
'lastName':'Aldrin'},
'mission':'apollo 11'}
Yep, it's definitely a classpath issue. You can compile your project without third-party libraries existing, but you cannot run it without those libraries on your classpath.
I have copied your code to my local system and verified that I receive the same (initial) Exception that you get. After grabbing the commons-lang-2.6.jar file and putting it on my classpath, I'm now receiving the following exception:
$ java com.so.Q8187623
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at net.sf.json.AbstractJSON.<clinit>(AbstractJSON.java:53)
at net.sf.json.util.CycleDetectionStrategy.<clinit>(CycleDetectionStrategy.java:36)
at net.sf.json.JsonConfig.<clinit>(JsonConfig.java:65)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
at com.so.Q8187623.main(Q8187623.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 5 more
So just follow your way down the rabbit-hole adding classes to your classpath...
The immediate error looks like it's because you're missing commons-lang.jar from your classpath. However, given that the class which can't be found is an exception, I strongly suspect that when you've fixed that error, you'll immediately get another one... in the form of a NestedRuntimeException being thrown.
I bumped into the same issue. I was using commons-collections 3.2.2 with commons-lang 2.6.
The issue got resolved after downgrading commons-collection to 3.2.1.