Java error: Unable to initialize main class
Asked Answered
S

1

9

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.

Shriver answered 25/4, 2020 at 17:35 Comment(3)
Check if all your dependencies are on the classpath.Abroad
@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?Shriver
I have no idea, but this might help - #5982209Abroad
C
5

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

Curlicue answered 25/4, 2020 at 17:43 Comment(2)
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?Shriver
check all connection properties such as uid/pwd/port. If it is remote then check the server configuration if it accepts remote connectionsCurlicue

© 2022 - 2024 — McMap. All rights reserved.