create a new ontology with Jena - classpath

I'm trying to use Jena. For creating a new ontology my code is:
String SOURCE = "http://www.w3.org/2002/07/owl#";
String NS = SOURCE + "#";
OntModel ontology = ModelFactory.createOntologyModel();
ontology.read( SOURCE, "OWL/XML" );
But it gives me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.hp.hpl.jena.util.Metadata.<clinit>(Metadata.java:26)
at com.hp.hpl.jena.JenaRuntime.<clinit>(JenaRuntime.java:25)
at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.<clinit>(RDFReaderFImpl.java:85)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.<clinit>(ModelCom.java:42)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:122)
at com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel(ModelFactory.java:116)
at com.hp.hpl.jena.vocabulary.OWL.<clinit>(OWL.java:37)
at com.hp.hpl.jena.ontology.ProfileRegistry.<clinit>(ProfileRegistry.java:48)
at com.hp.hpl.jena.ontology.OntModelSpec.<clinit>(OntModelSpec.java:54)
What's the problem?I couldn't find any solution for it.

If you use a Jena distribution, all the jars needed are in the lib/ directory. You need them all on the classpath.
On Windows / cygwin:
javac -cp '<install dir>\lib\*;' MyClass.java
On Linux:
javac -cp '<install dir>/lib/*' MyClass.java
To run, the created .class needs to be in your path, too:
java -cp '.:<install dir>/lib/*' MyClass
If you use maven to get Jena, the dependencies are automatically pulled in.

Your Java classpath is missing one of the jar files required by Jena. Looks like it's one of the slf4j jars. You need to have all the jar files that come with Jena on the classpath. How to set the classpath depends on your OS and/or IDE, but Google can help.

Related

how to add classpath to oozie java action with sharedlib and external jars

I have a java application which needs the library of hadoop, hdfs, hive and spark, also some external libraries,
I've read this page but I'm still confused about the order of overriding sharedlib,
in the job configure, I have
oozie.use.system.libpath=false
oozie.action.sharelib.for.java=spark,hive2,hive
I also put the external jars under the /lib of the workspace directory.
Now I got this problem, in my jar I used class from json4s-native, so I put them in the myworkspace/lib path, but under the oozie/share/lib/spark,also has library of json4s-jackson, after a run the java action, throw an error of
Launcher exception: java.lang.NoClassDefFoundError: org/json4s/native/JsonMethods$
How can I get oozie use the library in my /lib path first?

Cannot find parser that supports .groovy

When I'm trying to start changelog.groovy via liquibase command line it tells me that
Unexpected error running Liquibase: Cannot find parser that supports changelog.groovy
I'm doing the next: java -jar liquibase.jar update
My liquibase.properties are:
driver=org.postgresql.Driver
classpath=C:\Users\Andrii\org.postgresql.Driver.jar;C:\Users\Andrii\liquibase-3.5.1-bin\lib\liquibase-groovy-dsl-1.2.2-SNAPSHOT.jar
changeLogFile=D:\changelog.groovy
url=jdbc:postgresql://localhost:5432/test
username=postgres
password=rup
It finds those jars since if I change something in that path it will tell that jars cannot be found.
I downloaded the groovy-liquibase-dsl project, build it and added a jar into classpath. What am I doing wrong?
To make it work, you need to additionally include groovy and groovy-sql jars in Liquibase's classpath.
So say you store all the jars in C:\Users\Andrii\LiquibaseDependencies, update your the classpath property of your file as such:
classpath=C:\Users\Andrii\LiquibaseDependencies\org.postgresql.Driver.jar;
C:\Users\Andrii\LiquibaseDependencies\liquibase-groovy-dsl-1.2.1.jar;
C:\Users\Andrii\LiquibaseDependencies\groovy-2.4.6.jar;
C:\Users\Andrii\LiquibaseDependencies\groovy-sql-2.4.6.jar

Run junit4 test from cmd

I tried to run junit4 test case from command line using:
java -cp junit-4.8.1.jar;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
but I got the following error:
java.lang.NoClassDefFoundError: graph/shortestgraphpath;
while the test case is working without any problems in eclipse.
Hint: in eclipse, shortestgraphpath was added in Referenced Libraries.
You need to the jar file containing shortestgraphpath to java class path.
java -cp junit-4.8.1.jar;test\Dijkstra; test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
The class path is the value that you pass to java with -cp so in your question you just supply junitand your compiled classes.
Try updating it with the jar file with the missing class.
java -cp junit-4.8.1.jar;<path to jar file>;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
You might have to add additional jar files as well. I recommend that you take a look at some build tool to help you build and run your java applications for example Maven, Gradle, Buildr.

Eclipse JAR at runtime

I'm importing several JARs all sitting in the same directory. When my program runs I'm able to access the classes in several of those JARs, no problem. So my classpath is fine (I assume). But there is one JAR giving me trouble. When I try to run:
Configuration conf = new BaseConfiguration();
I get a NoClassDefFoundError error. The searching I did on "NoClassDefFound" typically points to classpath problems but as mentioned above, other JARs in the same directory are being found so I think that's not the problem.
This is in Eclipse 3.8.
The import statements:
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
And the name of the JAR is:
commons-configuration-1.6.jar
Any ideas?
Tom
Perhaps it's failing to load a dependency of one of the classes in commons-configuration-1.6.jar? Maybe from another commons- JAR? The NoClassDefFoundError should tell you which class the runtime failed to load. Have you checked this?

How o run a NetBeans-built Scala application jar from command line outside IDE?

A program of mine (written in Scala 2.8) works fine when launched by means of NetBeans IDE. But when I try to run it from outside, with "java- jar", it says "Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject...". Putting all the libraries, incl. Scala runtime inside the same dir as the jar to be run doesn't help. If i try to run the jar with scala itself, it complains that it can't decode it as utf-8 (it expects a scala source rather than a jar, I suppose). So how do I run a Scala application at all?
UPDATE: For those who come here later having the same question I'd recommend to read comments under barjak's answer (including those latest ones hidden), the answer is there. VonC also gives some interesting links on the subject.
The -jar and -classpath of the java command are mutually exclusive : you can't do java -jar YourScalaProg.jar -classpath scala-library.jar
If you want to run your application with java -jar, then the full classpath must be specified in the Class-Path section of the jar's manifest.
You can run your application using only -classpath, like that : java -classpath YourScalaProg.jar:scala-library.jar your.package.MainClass.
Are you using scala-library.jar as described in Adventures with Scala blog post?
java -classpath scala-library.jar:. YourClass
or:
java -classpath scala-library.jar:yourApp.jar YourClass
Where YourClass is was your scalac compiled Scala code.
You will find the same scala-library.jar used in the SO question "Creating a jar file from a Scala file" (or in the blog post "the not so elegant way of creating an executable jar from scala code").