I'm trying to write a simple Jena Fuseki client in Java to send a SELECT SPARQL query and get the result.
I've found the following code written by someone else. It keeps crashing at runtime.
I'm using jena-arq-3.1.0.jar
javac -cp .;jena-arq-3.1.0.jar Main.java print no errors
java -cp .;jena-arq-3.1.0.jar Main crash!
Error log
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/jena/atlas/io/Printable
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 org.apache.jena.query.QueryFactory.create(QueryFactory.java:78)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:52)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:40)
at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.jena.atlas.io.Printable
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)
... 16 more
Main.java
import java.net.URL;
import java.util.List;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
class Main {
public static void main(final String[] args) throws Exception {
String queryString=
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"prefix owl: <http://www.w3.org/2002/07/owl#>"+
"SELECT ?subject ?predicate ?object"+
"WHERE {"+
" ?subject ?predicate ?object"+
" } LIMIT 25";
// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:3030/FirstEndpoint/sparql", query);
//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {
// Result processing is done here.
}
}
finally {
qexec.close();
}
}
}
You need all necessary libs in the classpath, not only jena-arq.
javac works because you're using only classes from the jena-arq JAR.
java doesn't work because at runtime the classes of ARQ refer to other classes contain e.g. in jena-core, jena-iri, etc.
The easiest way would to add all libs from the Jena distribution resp. the lib folder itself to the classpath.
Related
I'm using APSTEX IFC Framework to creat a IFC 3D Viewer in my Java program.
My code is as follow.
import com.apstex.ifcjava3dviewer.IfcJava3DViewer;
public class JavaViewer2 {
public static void main(String[] args) {
IfcJava3DViewer ifcViewer = new IfcJava3DViewer();
}
}
The error is as followed:
java.lang.NoClassDefFoundError: com/jogamp/opengl/GLCapabilitiesImmutable
at com.apstex.javax.media.j3d.Pipeline$PipelineCreator.run(SourceFile:74)
at com.apstex.javax.media.j3d.Pipeline$PipelineCreator.run(SourceFile:1)
at java.security.AccessController.doPrivileged(Native Method)
at com.apstex.javax.media.j3d.Pipeline.b(SourceFile:91)
at com.apstex.javax.media.j3d.MasterControl.c(SourceFile:858)
at com.apstex.javax.media.j3d.VirtualUniverse.<clinit>(SourceFile:267)
at com.apstex.gui.core.controller.ApplicationController.<init>(SourceFile:1097)
at com.apstex.gui.core.kernel.Kernel.getApplicationController(SourceFile:59)
at com.apstex.ifcjava3dviewer.IfcJava3DPanel.<init>(SourceFile:71)
at com.apstex.ifcjava3dviewer.IfcJava3DViewer.<init>(SourceFile:38)
at test.main.JavaViewer2.main(JavaViewer2.java:7)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLCapabilitiesImmutable
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)
... 11 more
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.apstex.javax.media.j3d.VirtualUniverse
at com.apstex.javax.media.j3d.Canvas3D.<clinit>(SourceFile:3821)
at com.apstex.gui.ifc.views.view3d.j3d.ModelViewer.<init>(SourceFile:93)
at com.apstex.gui.ifc.views.view3d.j3d.ModelViewer.<init>(SourceFile:79)
at com.apstex.ifcjava3dviewer.IfcJava3DPanel.<init>(SourceFile:1079)
at com.apstex.ifcjava3dviewer.IfcJava3DViewer.<init>(SourceFile:38)
at test.main.JavaViewer2.main(JavaViewer2.java:7)
How can I correct the error?
Thanks for the two commends above to help me solve the problem. By installing a library of JOGL the program works.
#PrepareForTest({ A.class, B.class, SomeJavaClass.class, D.class})
class ReportActionTest
{
// JUnit class having around 10,000 lines of code
}
class SomeJavaClass
{
//Java class having around 10,500 lines of code
}
All JUnits of ReportActionTest were executing properly until I added few new methods in SomeJavaClass.
After Adding new methods, I tried to run JUnits of ReportActionTest.
But ReportActionTest couldn't load.
Following is the error log :
java.lang.ClassFormatError: Invalid this class index 11012 in constant pool in class file com/app/reports/web/action/SomeJavaClass
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:210)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:145)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at junit.framework.TestSuite.getTestConstructor(TestSuite.java:83)
at org.powermock.modules.junit3.internal.impl.PowerMockJUnit3RunnerDelegateImpl.<init>(PowerMockJUnit3RunnerDelegateImpl.java:54)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit3TestSuiteChunkerImpl.java:75)
at org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit3TestSuiteChunkerImpl.java:37)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217)
at org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.<init>(JUnit3TestSuiteChunkerImpl.java:46)
... 20 more
Then I Removed some of the old methods(not newly added ones) of SomeJavaClass.class(Just to reduce it's size).
Then I tried to run JUnits of ReportActionTest and ReportActionTest loaded properly.
So, I wanted to know, is there any limitation on size of the classes added in #PrepareForTest ?
I want to understand this behaviour, if anyone can explain.
I am trying to write a little test-programm with different Databases. MongoDB is one of them and it worked perfectly fine until last Friday, even though I didn't change anything in the java code of my MongoDB class.
System: I use an ubuntu 14.04 VM with the package mongo-org. The version of mongo is 3.0.5.
I also run MariaDB, PostgreSQL and Cassandra on the VM.
As a Client, I have windows 7 and Eclipse.
The connection and the test work perfectly fine with the other 3 databases.
And I can run the "mongo" client on ubuntu as well.
But when I try out the Java Test of MongoDB, I keep getting this error:
Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class
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 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.mongodb.MongoClientOptions$Builder.<init>(MongoClientOptions.java:52)
at com.mongodb.MongoClient.<init>(MongoClient.java:126)
at com.mongodb.MongoClient.<init>(MongoClient.java:115)
at de.motza.database.impl.DatabaseImplMongoDB.connect(DatabaseImplMongoDB.java:197)
at de.motza.database.impl.DatabaseImplMongoDB.instantiate(DatabaseImplMongoDB.java:207)
at de.motza.test.History_Log.testNewDatabase(History_Log.java:86)
at de.motza.test.History_Log.main(History_Log.java:66)
The part of the java Class, where the Exception occurs:
private static String dbHost = "10.158.251.251";
private static int dbPort = 27017;
private static String dbName = "history_log";
private static MongoClient mongoClient = null;
[...]
#Override
public void connect() {
try {
mongoClient = new MongoClient(dbHost, dbPort);
db = mongoClient.getDB(dbName);
} catch (Exception e) {
log.error(e);
}
}
I read about the disk-space, but I habe 12 GB free in my VM, so I do not think that this is the problem. I ran apt-get upgrade, but it didn't change anything either. I also reinstalled mongoDb, but nothing changed.
I also tried to recompile, but nothing changed there either.
Does anybody have any advice?
ADDITION:
Here you find the library tab of my project. I use multiple databases, so I marked the ones important for MongoDB
One possible reason for a IncompatibleClassChangeError you get is that a Mongo library binary has changed (for whatever reason) and now your earlier compiled test code isn't any more compatible with the library. I would suggest to recompile your test code and try again.
I am using the Java RSS parser horrorss
I added horrorss-2.2.0.jar to my Build path in Eclipse.
I can't even get the Simplest bit of Code to run.
import org.horrabin.horrorss.*;
public class Test {
public static void main(String[] args) throws Exception {
RssParser rss = new RssParser();
try{
RssFeed feed = rss.load("http://rss.slashdot.org/Slashdot/slashdot");
// Gets the channel information of the feed and
// display its title
RssChannelBean channel = feed.getChannel();
System.out.println("Feed Title: " + channel.getTitle());
}catch(Exception e){
System.err.println(e);
}
}
}
The Console Output is always the following
Exception in thread "main" java.lang.NoClassDefFoundError: com/hp/hpl/sparta/Parser
at org.horrabin.horrorss.RssParser.parseFromReader(RssParser.java:431)
at org.horrabin.horrorss.RssParser.parseFromURL(RssParser.java:455)
at org.horrabin.horrorss.RssParser.load(RssParser.java:115)
at org.horrabin.horrorss.RssParser.load(RssParser.java:147)
at Test.main(Test.java:10)
Caused by: java.lang.ClassNotFoundException: com.hp.hpl.sparta.Parser
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)
... 5 more
I hope you guys are able to help me out (or show me another nice method for Handling RSS Feeds in Java)
Did you read this on the homepage?
It requires Sparta-XML, a light-weight XML parser based on XPath.
You need to get the Sparta-XML jar and add that to the eclipse build path too.
I have read the questions on using Sqoop from within a Java program here, here and here.
I came up with the following, but I am stumped by a ClassNotFoundException:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import com.cloudera.sqoop.SqoopOptions;
import com.cloudera.sqoop.SqoopOptions.FileLayout;
import com.mysql.jdbc.*;
public class SqoopExample {
public static void main(String[] args) throws Exception {
String driver = "com.mysql.jdbc.driver";
Class.forName(driver);
Configuration config = new Configuration();
// note that this is HDFS path, rather than core path
config.addResource(new Path("./config/core-site.xml"));
config.addResource(new Path("./config/hdfs-site.xml"));
FileSystem dfs = FileSystem.get(config);
SqoopOptions options = new SqoopOptions();
options.setDriverClassName(driver);
options.setConnectString("jdbc:mysql://localhost:3306/dbname");
options.setTableName("v_webstats");
options.setUsername("root");
options.setNumMappers(1);
options.setTargetDir(dfs.getWorkingDirectory()+"/TestDirectory");
options.setFileLayout(FileLayout.TextFile);
new com.cloudera.sqoop.tool.ImportTool().run(options);
}
}
I am sure that all the jars are correctly included and I have tested the connection to my database server and that works as well.
The exact error is:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
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 java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at SqoopExample.main(SqoopExample.java:14)
not sure what I am doing wrong.
I believe that proper name of the MySQL JDBC driver class is:
com.mysql.jdbc.Driver
(notice the capital "D" in the "Driver)