I am using the Apertium Translator and using the sample code they provide. My code is like this.
import com.robtheis.aptr.language.Language;
import com.robtheis.aptr.translate.Translate;
public class Test {
public static void main(String[] args) throws Exception {
// Set the Apertium API Key - Get yours at http://api.apertium.org/register.jsp
Translate.setKey("BNSCFhEL8DoTApc2I1+aa3UYkVg");
String translatedText = Translate.execute("Hola, mundo!", Language.SPANISH, Language.ENGLISH);
System.out.println(translatedText);
}
}
I have no errors or warnings and when I run the program I get the following errors.
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONValue
at com.robtheis.aptr.ApertiumTranslatorAPI.jsonSubObjToString(ApertiumTranslatorAPI.java:195)
at com.robtheis.aptr.ApertiumTranslatorAPI.retrieveSubObjString(ApertiumTranslatorAPI.java:140)
at com.robtheis.aptr.translate.Translate.execute(Translate.java:56)
at maple.Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONValue
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)
... 4 more
The .jar I'm using is the second one from https://github.com/rmtheis/apertium-translator-java-api/downloads
You have to download the first one. The first jar file (apertium-translator-java-api-0.2-jar-with-dependencies.jar) contains the all dependencies needed..
Or you add a json library to your project path..
Well JVM is not able to find some class at Runtime which was available at compile time. That is the reason of NoClassDefFoundError
Below you also have ClassNotFoundException for this class org.json.simple.JSONValue
which means you are trying to load the particular class at run-time by name.
Both of them related to Java Class Path.
I don't know alot about jSON stuff. But you are missing this file org.json.simple.JSONValue you can take it from here http://code.google.com/p/json-simple/
add the above jar file in your class path . And the code will definitely run. Guaranteed.!!!
Thanks
Related
I am trying to load an ontology saved on my PC. The file format is .n3.
After a lot of work and searching for the possible solutions, I didn't find an appropriate way to load an ontology.
I tried the following code:
import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.model.OWLModel;
public class Extractor {
public static void main(String[] args) {
String ontologyURI = "file:///C:/acco.n3";
try {
OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(ontologyURI);
System.out.println("Worked");
}catch (Exception exception) {
System.out.println("Error can't upload the ontologie ");
}
}
}
but it always gives me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at com.hp.hpl.jena.graph.Node.<clinit>(Node.java:35)
at com.hp.hpl.jena.rdf.model.impl.ResourceImpl.fresh(ResourceImpl.java:108)
at com.hp.hpl.jena.rdf.model.impl.ResourceImpl.<init>(ResourceImpl.java:68)
at com.hp.hpl.jena.rdf.model.ResourceFactory$Impl.createResource(ResourceFactory.java:176)
at com.hp.hpl.jena.rdf.model.ResourceFactory.createResource(ResourceFactory.java:69)
at com.hp.hpl.jena.vocabulary.RDF.resource(RDF.java:29)
at com.hp.hpl.jena.vocabulary.RDF.<clinit>(RDF.java:37)
at com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType.<clinit>(XMLLiteralType.java:30)
at com.hp.hpl.jena.datatypes.TypeMapper.<clinit>(TypeMapper.java:52)
at edu.stanford.smi.protegex.owl.model.impl.OWLSystemFrames.createRDFSDatatypes(Unknown Source)
at edu.stanford.smi.protegex.owl.model.impl.OWLSystemFrames.createOWLInstances(Unknown Source)
at edu.stanford.smi.protegex.owl.model.impl.OWLSystemFrames.createOWLMetaModel(Unknown Source)
at edu.stanford.smi.protegex.owl.model.impl.OWLSystemFrames.<init>(Unknown Source)
at edu.stanford.smi.protegex.owl.swrl.SWRLSystemFrames.<init>(Unknown Source)
at edu.stanford.smi.protegex.owl.model.impl.AbstractOWLModel.createSystemFrames(Unknown Source)
at edu.stanford.smi.protegex.owl.model.impl.AbstractOWLModel.createSystemFrames(Unknown Source)
at edu.stanford.smi.protege.model.DefaultKnowledgeBase.<init>(DefaultKnowledgeBase.java:79)
at edu.stanford.smi.protegex.owl.model.impl.AbstractOWLModel.<init>(Unknown Source)
at edu.stanford.smi.protegex.owl.jena.JenaOWLModel.<init>(Unknown Source)
at edu.stanford.smi.protegex.owl.jena.JenaKnowledgeBaseFactory.createKnowledgeBase(Unknown Source)
at edu.stanford.smi.protege.model.Project.createDomainKB(Project.java:429)
at edu.stanford.smi.protege.model.Project.createDomainKnowledgeBase(Project.java:447)
at edu.stanford.smi.protegex.owl.jena.creator.OwlProjectFromUriCreator.create(Unknown Source)
at edu.stanford.smi.protegex.owl.ProtegeOWL.createJenaOWLModelFromURI(Unknown Source)
at Extractor.main(Extractor.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 25 more
I need your help in solving this error, or perhaps find a way to load ontologies to extract knowledge from them.
Thanks in advance.
Update:
I added the missing dependencies to the class path as #Roddy of the Frozen Peas suggested.
but it gives me the following error:
WARNING: [Local Folder Repository] The specified file must be a directory. (C:\eclipse-workspace\NewEx\plugins\edu.stanford.smi.protegex.owl) -- LocalFolderRepository.update()
Exception in thread "main" java.lang.NoClassDefFoundError: org/protege/editor/owl/model/hierarchy/roots/Relation
at edu.stanford.smi.protegex.owl.jena.parser.TripleProcessor.<init>(Unknown Source)
at edu.stanford.smi.protegex.owl.jena.parser.GlobalParserCache.getTripleProcessor(Unknown Source)
at edu.stanford.smi.protegex.owl.jena.parser.ProtegeOWLParser.loadTriples(Unknown Source)
at edu.stanford.smi.protegex.owl.jena.parser.ProtegeOWLParser.run(Unknown Source)
at edu.stanford.smi.protegex.owl.jena.JenaKnowledgeBaseFactory.loadKnowledgeBase(Unknown Source)
at edu.stanford.smi.protege.model.Project.loadDomainKB(Project.java:1346)
at edu.stanford.smi.protege.model.Project.createDomainKnowledgeBase(Project.java:456)
at edu.stanford.smi.protegex.owl.jena.creator.OwlProjectFromUriCreator.create(Unknown Source)
at edu.stanford.smi.protegex.owl.ProtegeOWL.createJenaOWLModelFromURI(Unknown Source)
at Extractor.main(Extractor.java:10)
Caused by: java.lang.ClassNotFoundException: org.protege.editor.owl.model.hierarchy.roots.Relation
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 10 more
any suggestions please?
java.lang.ClassNotFoundException: org.protege.editor.owl.model.hierarchy.roots.Relation
There are other dependencies you need. Rather than going piecemeal, you should use a build system that can resolve the dependency closure for you, such as Maven, Gradle or similar.
The problem here is not Protege specific or ontology related, it's just plain Java libraries missing from the classpath.
Note that your code does not use OWL API. These classes are Jena classes and Protege classes (from Protege 3.x, I'd think). Note that Protege 3.x is a very dated API. Consider using a newer version (5.2.0 or newer), or directly using the actual OWL API. See https://github.com/owlcs/owlapi/wiki/Documentation
OWL API also has zip files containing all dependencies, which might simplify your work (Maven is still better than resolving dependencies by hand). See https://github.com/owlcs/releases/tree/master/owlapi/5
I have simple Hello word program. Program will compile and run when not declare namespace in code but when I declared the class within namespace and compile the program it will complie successfully but it will given an error classnotfound when this program run. My question is why java complier not availble to find class when declare namespace (package) in code?
Please find below source code:
package org;
public class Chunk
{
public static void main(String [] args)
{
System.out.println("Hello, World");
}
}
and command is
java org.Chunk
and error is
java.lang.NoClassDefFoundError: org/Chunk
Caused by: java.lang.ClassNotFoundException: org.Chunk
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)
Could not find the main class: org.Chunk. Program will exit.
Exception in thread "main"
You are getting this error probably because the class is not part of the classpath. You can specify a classpath using -cp java option, to point to the directory/jar where the org.Chunk class is found.
When I tried to run Java on Linux on the terminal this is what happens:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class
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)
Can anyone help me with this?
EDIT: I was in the folder of the bytecode file and ran this on the terminal:
bash-4.1$ java class HelloWorldApp
Source file:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
You are executing
java HelloWorldApp.class
but it must be
java HelloWorldApp
You may not append class to your call but name plain the classname.
Also, as others has remarked, it is better to use packages as classes in the default package do not work fine.
You also should note that if you have the package declaration in the code it will screw it up even if you try all the fancy fixes like setting CLASSPATH. For example if you have:
package blah;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
The line: package blah;
will cause java HelloWorld to fail after compiling. So remove this line and you should be able to run the src via cmd line.
I'm new to java and i have a problem while trying to run my program. I'm using eclipse.
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String imie;
Scanner odczyt = new Scanner(System.in);
imie=odczyt.nextLine();
System.out.println("Witaj "+imie);
}}
This what I get:
EDIT(Runned without dot)
Exception in thread "main" java.lang.NoClassDefFoundError: Scan
Caused by: java.lang.ClassNotFoundException: Scan
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)
Could not find the main class: Scan. Program will exit.
Please help me. :)
When you're running java, don't put .java on the end of the class name.
It looks like you're trying to run a class named Scan.java, but there is no such class; the class is just named Scan. However you're launching your class, you need to launch just Scan, i.e.,
java Scan
not
java Scan.java
You need to specify classpath that specifies where Scan class can be found. The classpath can point to a folder of classes or a JAR file. For instance...
java -cp scan.jar Scan
My program below is called Apple.java
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
int y=20,z=0;
z=y+10;
System.out.println("x+y is "+z);
}
}
This is another program called Fruit.java
package food;
public abstract class Fruit{
public static void main(String[] args){
int x=10;
System.out.println("x is "+x);
}
}
Both these programs are stored in the following directory path :- G:\Java\food
I did successfully compile both the programs using the following command:-
G:\>javac Java\food\*.java
and it produced class files.
I did manage to execute the Fruit program using the following command:-
G:\>java -classpath G:\Java food.Fruit
However, when I tried to do the same for the Apple program it resulted in error messages.
G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)
Could someone please help me in figuring out what the error is? I am trying to get myself ready for the SCJP cert exam and hence I reference this example back to the McGrawHill publication book written by Kathy Sierra and Bert Bates.
Edit #2
Yes, I did because that was how the code was in the book. Is there a way I could still execute the file without having to include the package statement.
This is what the question looks like:
Attempt to compile the two files. If you want to use the Apple class, make
sure you place the Fruit.class file in the food subdirectory.
Edit #3
I did try that and got the following errors
G:\>java -classpath G:\Java\food Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Fruit
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
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$000(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)
Caused by: java.lang.ClassNotFoundException: food.Fruit
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)
... 12 more
Could not find the main class: Apple. Program will exit.
Edit #4
I did manage to execute it without having to include the statement
package food;
G:\>java -classpath G:\Java\food;G:\Java Apple
x+y is 30
What I changed was I moved the Apple.class file into a separate folder which makes sense because the folder "food" is an indication of the package and as Darvis pointed all files in the folder needed to have the package name else it would not execute.
Edit #5
This worked too
G:\>java -classpath G:\Java Apple
x+y is 30
because you haven't specified food package for that. make it like this and it will work
package food;
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
int y=20,z=0;
z=y+10;
System.out.println("x+y is "+z);
}
}
Update:
When you execute below
G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)
it will add G:\Java in class path from there it will relatively search for class apple in food DIR with package name specified food
Or. if you don't want to specify package in the class then
G:\>java -classpath G:\Java\food Apple