title: I'm using Jena to launch a SPARQL query. The query works in DBpedia SPARQL endpoint! I verified the query at query validator and got the same error
body: <p>I need to find movies which are similar to movie for e.g.lagaan based on the common properties.I tried the code below but i am getting the below  Exception when i use jena though the query works fine in sparql endpoint.</p>
  
  <p>Output in sparql endpoint:</p>
  
  <pre><code>similar movies                                  similarity score 
  http://dbpedia.org/resource/Lagaan                 177
  http://dbpedia.org/resource/Jodhaa_Akbar            44
  http://dbpedia.org/resource/Jaane_Tu..._Ya_Jaane_Na 42
  http://dbpedia.org/resource/Swades                  42
  http://dbpedia.org/resource/Rangeela_(film)         40
  http://dbpedia.org/resource/Dil_Ne_Jise_Apna_Kahaa  38
  http://dbpedia.org/resource/Love_You_Hamesha        37
  http://dbpedia.org/resource/Sholay                  37
  http://dbpedia.org/resource/Kannathil_Muthamittal   36
  http://dbpedia.org/resource/Andaaz                  36
  http://dbpedia.org/resource/Jaan-E-Mann             36
  http://dbpedia.org/resource/Sarfarosh               36
  http://dbpedia.org/resource/Saathiya_(film)         36
  http://dbpedia.org/resource/Sillunu_Oru_Kaadhal     36
  http://dbpedia.org/resource/Doli_Saja_Ke_Rakhna     36
  http://dbpedia.org/resource/Dil_Se..                36
  http://dbpedia.org/resource/Rang_De_Basanti         36
  http://dbpedia.org/resource/Lage_Raho_Munna_Bhai    36
  http://dbpedia.org/resource/Ishq_Vishk              36
  </code></pre>
  
  <p>For any other query which i tried i m getting the same exception error though that query is working fine in sparql endpoint and Query validator. I tried the solution given in the below link <a href="https://stackoverflow.com/questions/19690335/sparql-parse-error-with-jena-but-dbpedia-accepts-the-query?answertab=oldest#tab-top">SPARQL parse error with Jena, but DBpedia accepts the query</a> but is doesnt work for me.</p>
  
  <pre><code>import com.hp.hpl.jena.query.QueryExecution;
  import com.hp.hpl.jena.query.QueryExecutionFactory;
  import com.hp.hpl.jena.query.QuerySolution;
  import com.hp.hpl.jena.query.ResultSet;
  
  public class Dbpedia {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          String service="http://dbpedia.org/sparql";
          String query= " PREFIX dbpedia: &lt;http://dbpedia.org/resource/&gt; "
                  + "PREFIX dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt; "
                  + "select ?similar (count(?p) as ?similarity) "
                  + "where { values ?movie { dbpedia:Lagaan }"
                  + " ?similar ?p ?o ; a dbpedia-owl:Film . "
                  + "?movie   ?p ?o .} group by "
                  + "?similar ?movie having count(?p) &gt; 35 order by desc(?similarity)";
          QueryExecution e=QueryExecutionFactory.sparqlService(service, query);
          ResultSet rs=e.execSelect();
          while (rs.hasNext()) {
              QuerySolution qs=rs.nextSolution();
              System.out.println(qs);
          }
      }
  
  }
  </code></pre>
  
  <p>ERROR:</p>
  
  <pre><code>Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "&gt;" "&gt; "" at line 1, column 350.
  Was expecting one of:
      &lt;EOF&gt; 
      &lt;IRIref&gt; ...
      &lt;PNAME_NS&gt; ...
      &lt;PNAME_LN&gt; ...
      "limit" ...
      "offset" ...
      "order" ...
      "values" ...
      "exists" ...
      "not" ...
      "count" ...
      "min" ...
      "max" ...
      "sum" ...
      "avg" ...
      "sample" ...
      "group_concat" ...
      "bound" ...
      "coalesce" ...
      "if" ...
      "bnode" ...
      "iri" ...
      "uri" ...
      "str" ...
      "strlang" ...
      "strdt" ...
      "datatype" ...
      "lang" ...
      "langmatches" ...
      "isURI" ...
      "isIRI" ...
      "isBlank" ...
      "isLiteral" ...
      "isNumeric" ...
      "regex" ...
      "sameTerm" ...
      "RAND" ...
      "ABS" ...
      "CEIL" ...
      "FLOOR" ...
      "ROUND" ...
      "CONCAT" ...
      "SUBSTR" ...
      "STRLEN" ...
      "REPLACE" ...
      "UCASE" ...
      "LCASE" ...
      "ENCODE_FOR_URI" ...
      "CONTAINS" ...
      "STRSTARTS" ...
      "STRENDS" ...
      "STRBEFORE" ...
      "STRAFTER" ...
      "YEAR" ...
      "MONTH" ...
      "DAY" ...
      "HOURS" ...
      "MINUTES" ...
      "SECONDS" ...
      "TIMEZONE" ...
      "TZ" ...
      "NOW" ...
      "UUID" ...
      "STRUUID" ...
      "MD5" ...
      "SHA1" ...
      "SHA256" ...
      "SHA384" ...
      "SHA512" ...
      "(" ...
  
      at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
      at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
      at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
      at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
      at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
      at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
      at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
      at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:311)
      at com.hp.hpl.jena.query.QueryExecutionFactory.sparqlService(QueryExecutionFactory.java:298)
      at com.wiki.Dbpedia.main(Dbpedia.java:22)
  </code></pre>
  
  <p>I tried removing the group by clause which is validated by query validator and gives some output at sparql endpoint but again same exception when i run it in eclipse </p>
  
  <pre><code>" PREFIX  dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt; " +
            " PREFIX  dbpedia: &lt;http://dbpedia.org/resource/&gt; " +
                " SELECT  ?similar " +
                " WHERE " +
                " { VALUES ?movie { dbpedia:Lagaan } " + 
                " ?similar ?p ?o ."+
                " ?similar &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt; dbpedia-owl:Film ." +
                " ?movie ?p ?o " +
                " } ";
  </code></pre>
  
  <p>CHANGES:`
   query validator returns the following query output when i added () to having as said by @AndyS</p>
  
  <pre><code>1 PREFIX  dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt;
    2 PREFIX  rdf:  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
    3 
    4 SELECT  ?similar (count(?p) AS ?similarity)
    5 WHERE
    6   { VALUES ?movie { &lt;http://dbpedia.org/resource/Lagaan&gt; }
    7     ?similar ?p ?o .
    8     ?similar rdf:type dbpedia-owl:Film .
    9     ?movie ?p ?o
   10   }
   11 GROUP BY ?similar ?movie
   12 HAVING ( count(?p) &gt; 35 )
   13 ORDER BY DESC(?similarity)
  </code></pre>
  
  <p>I changed query to following in eclipse but again the same error.</p>
  
  <pre><code>String query= "PREFIX  dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt;"+
                "PREFIX  rdf:  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;"+
                "SELECT  ?similar (count(?p) AS ?similarity)" +
                        "WHERE" +
                        "{ VALUES ?movie { &lt;http://dbpedia.org/resource/Lagaan&gt; }"+
                        "?similar ?p ?o ."+
                        "?similar rdf:type dbpedia-owl:Film ."+
                        "?movie ?p ?o"+
                        "}"+
                        "GROUP BY ?similar ?movie"+
                        "HAVING ( count(?p) &gt; 35 )"+
                        "ORDER BY DESC(?similarity)";
  </code></pre>
  
  <p>Corrected Query:      </p>
  
  <pre><code>String query="PREFIX dbpprop: &lt;http://dbpedia.org/property/&gt; "
              + " PREFIX dbpedia: &lt;http://dbpedia.org/resource/&gt; "
              + "PREFIX dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt; "
              + "select ?similar (count(?p) as ?similarity) "
              + "where { values ?movie { &lt;http://dbpedia.org/resource/Lagaan&gt; }"
              + " ?similar ?p ?o ; a dbpedia-owl:Film . "
              + "?movie   ?p ?o .} group by "
              + "?similar ?movie having(count(?p) &gt; 35) order by desc(?similarity)";
  </code></pre>
  
  <p>new query to find movie link with movie name:</p>
  
  <pre><code>prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
  prefix dbpedia-owl: &lt;http://dbpedia.org/ontology/&gt;
   SELECT  *WHERE
      {   
      { 
    select distinct ?film where  {
    ?film a dbpedia-owl:Film .
    ?film rdfs:label ?label .
    filter regex( str(?label), "Lagaan", "i")
    }
    limit 10
     }
  </code></pre>
  
  <p>now how to pass the output of this query to the similarity query?</p>
  
  <p>Modified query using wikiredirects to handle mispelled movie names as suggested by @Joshau Taylor:</p>
  
  <pre><code>PREFIX  rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
  PREFIX  dbo:  &lt;http://dbpedia.org/ontology/&gt;
  PREFIX  owl:  &lt;http://www.w3.org/2002/07/owl#&gt;
  SELECT  ?s ?other (count(*) AS ?similarity)
  WHERE
  { 
  { 
  SELECT  ?s WHERE { 
    { ?s rdfs:label "Veer zara"@en .
    ?s &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt; owl:Thing
  }
  UNION
     { ?altName rdfs:label "Veer zara"@en .
      ?altName dbo:wikiPageRedirects ?s
      }
   }
   }
       ?s ?p ?o .
       ?other &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&gt; dbo:Film .
       ?other ?p ?o
     }
  GROUP BY ?s ?other
  HAVING ( count(?p) &gt; 25 )
  ORDER BY DESC(?similarity)
  </code></pre>