7

I am trying to run this code but I get the following error.

package practicaXML;
import javax.xml.xquery.*;
import org.w3c.dom.Node;
import net.xqj.basex.BaseXXQDataSource;

public class App {
    public static void main(String[] args) {
        try {
            XQDataSource xqs = new BaseXXQDataSource();
            xqs.setProperty("serverName", "localhost");
            xqs.setProperty("port", "1984");
            xqs.setProperty("databaseName", "facts");

            XQConnection conn = xqs.getConnection("admin", "admin");

            String xqueryString = "//province[contains(@name, 'x')]";
            XQExpression xqe = conn.createExpression();
            XQResultSequence rs = xqe.executeQuery(xqueryString);

            Node n;
            while(rs.next()) {
                n = rs.getNode();
                System.out.println(n.getAttributes().getNamedItem("name").getNodeValue());
            }

            conn.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Console:

Error: Unable to initialize main class practicaXML.App
Caused by: java.lang.NoClassDefFoundError: javax/xml/xquery/XQDataSource

It is my first time working with basex and xpath and I have no idea what could be causing this or how to fix it.

winter
  • 207
  • 1
  • 4
  • 13
  • Check if all your dependencies are on the classpath. – user Apr 25 '20 at 17:37
  • @user I have fixed the dependencies and now I get the following error: Error: Connection refused: no further information. Do you know what could that be? – winter Apr 25 '20 at 17:47
  • I have no idea, but this might help - https://stackoverflow.com/questions/5981709/jax-ws-java-net-connectexception-connection-refused – user Apr 25 '20 at 17:48

1 Answers1

4

You are missing dependencies. Add dependencies to the classpath or maven repository

user895565
  • 101
  • 5
  • I have fixed the dependencies and now I get the following error: Error: Connection refused: no further information. Do you know what could that be? – winter Apr 25 '20 at 17:47
  • 1
    check all connection properties such as uid/pwd/port. If it is remote then check the server configuration if it accepts remote connections – user895565 Apr 25 '20 at 17:53