-1

Whenver I run this program, its always throwing this error

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

And while I was trying run this code in Eclipse IDE it's telling me

declared package org.exist.examples.xmldb does not match the expected package

When I compile it it's throwing the error at Put.main(Put.java:27)

package org.exist.examples.xmldb;

import java.io.File;

import org.exist.xmldb.XmldbURI;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;

/**
 * Add a document to the database.
 * 
 * Call with java -jar start.jar org.exist.examples.xmldb.Put collection docName
 *
 */
public class Put {

    public final static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";

    protected static void usage() {
        System.out.println("usage: org.exist.examples.xmldb.Put collection docName");
        System.exit(0);
    }

    public static void main(String args[]) throws Exception {
        if(args.length < 2)
            usage();

        String collection = args[0], file = args[1];

        // initialize driver
        String driver = "org.exist.xmldb.DatabaseImpl";
        Class<?> cl = Class.forName(driver);            
        Database database = (Database)cl.newInstance();
        database.setProperty("create-database", "true");
        DatabaseManager.registerDatabase(database);

        // try to get collection
        Collection col = 
            DatabaseManager.getCollection(URI + collection);
        if(col == null) {
            // collection does not exist: get root collection and create.
            // for simplicity, we assume that the new collection is a
            // direct child of the root collection, e.g. /db/test.
            // the example will fail otherwise.
            Collection root = DatabaseManager.getCollection(URI + XmldbURI.ROOT_COLLECTION);
            CollectionManagementService mgtService = 
                (CollectionManagementService)root.getService("CollectionManagementService", "1.0");
            col = mgtService.createCollection(collection.substring((XmldbURI.ROOT_COLLECTION + "/").length()));
        }
        File f = new File(file);
        // create new XMLResource
        XMLResource document = (XMLResource)col.createResource(f.getName(), "XMLResource");
        document.setContent(f);
        System.out.print("storing document " + document.getId() + "...");
        col.storeResource(document);
        System.out.println("ok.");
    }
}
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
Suresh
  • 111
  • 1
  • 1
  • 6
  • Does your directory structure match the package structure? – Vivin Paliath Nov 11 '13 at 18:12
  • Well, does your actual folder structure reflects this path? `org/exist/examples/xmldb`? – Pradeep Simha Nov 11 '13 at 18:12
  • 1
    That means that the Java source file is stored in a directory that doesn't match with the package tree. If the package is org.exist.examples.xmldb, the file must be, in the sources directory, in the directory org/exist/examples/xmldb. Don't try to run code that doesn't even compile. – JB Nizet Nov 11 '13 at 18:13
  • May be you copy pasted the code and forgot to match the package :) – constantlearner Nov 11 '13 at 18:18
  • Ya by adding that "org.exist.examples.xmldb;" to my current program it's running fine now – Suresh Nov 12 '13 at 10:54

1 Answers1

0

You are trying to run code that does not compile. Eclipse inserts bytecode that throws this error instead.

Demi
  • 3,535
  • 5
  • 29
  • 45