Firstly, I found this relevant question and answer:
If I add the PREFIX in the SPARQL query it works. However, I don't want to duplicate all the prefixes in all my SPARQL queries, but to define them only once. I tried to do it programmatically for rdfs:
:
model.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
query.setPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
and it works, but if I try to do it with my own ontologies it doesn't work:
model.setNsPrefix("myOnt", "http://example.com/ontologies/myOnt#");
query.setPrefix("myOnt", "http://example.com/ontologies/myOnt#");