0

I want to ask my data.rdf RDF file using an SPARQL query in java

For this I used the Jena API, here is my source code:

import org.apache.log4j.chainsaw.Main;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
public class HelloRDFWorld {
    public static void main(String[] args) {
        FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model=FileManager.get().loadModel("D:/workspace_java/JenaTutorial/src/data.rdf");

        String querystring=
                "Prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
                "PREFIX FOAF: <http://xmlns.com/foaf/0.1/>"+
                "select *  where"+
                "{"+
                "?person foaf:name ?"+
                "}";
        Query query = QueryFactory.create(querystring);
        QueryExecution qexec= QueryExecutionFactory.create(query);
        try
        {
            ResultSet result= qexec.execSelect();
            while(result.hasNext()){
                QuerySolution sol= result.nextSolution();
                Literal name= sol.getLiteral("x");
                System.out.println(name);   
            }
        }
        finally{
            qexec.close();
        }

    }
}

and this is my RDF file:

<rdf:RDF>
  <foaf:person>
    <foaf:name>gorge</foaf:name>
  </foaf:person>
  <foaf:person>
    <foaf:name>Johon</foaf:name>
  </foaf:person>
</rdf:RDF>

After execution; it shows me this exception

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/hp/hpl/jena/util/FileManager : Unsupported major.minor version 51.0
    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)
    at tutorial.HelloRDFWorld.main(HelloRDFWorld.java:19)

so where is the problem?

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52
Sitw Sic
  • 35
  • 6

1 Answers1

0

You have tried to run a library (in this case Jena) compiled against Java 7 in an older JVM. You have three options:

  1. Use a supported JVM (Java 6 and before are no longer supported by Oracle)
  2. Recompile the library you want to use using the Java you use (might be not possible, as it might use APIs from Java 7).
  3. Try some Java backporting tool (but it might still fail at runtime if there are APIs used from higher JDKs). (Though in your case there seems to be no tool to retro from 7 to 6 or 5.)

The first option is strongly recommended (fastest and safest, probably most supported option).

Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52