Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java error: Unable to initialize main class

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.

like image 527
winter Avatar asked Apr 25 '20 17:04

winter


People also ask

Could not find main class Java error?

The "Could not find or load main class" error occurs when the JVM fails to load the main class. This can happen due to various reasons, such as: The class being declared in the incorrect package. The file path of the class not matching the fully qualified name.

How do you fix could not find or load main class?

There are many ways Error: Could not find or load main class HelloWorld manifests itself, but if you know the basics of Java Classpath, you can easily sort out the problem. Most of the time you just need to either correct your CLASSPATH environment variable or run your program with java -cp or -classpath option.

Could not find or load main class HelloWorld class?

Wrong Class Name And it failed with the error “Could not find or load main class helloworld.” As discussed earlier, the compiler will generate the . class file with the exact same name given to the Java class in the program. So in our case, the main class will have the name HelloWorld, not helloworld.


1 Answers

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

like image 51
user895565 Avatar answered Sep 24 '22 08:09

user895565