gpt4 book ai didi

java - Db4o API 在 NetBeans 中看不到动态加载的类?

转载 作者:行者123 更新时间:2023-12-01 15:16:48 25 4
gpt4 key购买 nike

认为似乎在使用 db4o 和 NetBeans 时遇到了类加载器问题。当我使用 .../jre/lib/ext 中的相同 jar 文件从终端运行完全相同的代码时,一切正常。问题是,当我对使用类加载器在运行时加载的某些类进行 native 查询时,我从数据库中得到一个空列表,我肯定应该在其中获取包含一些元素的列表(如我说过,相同的代码在命令行中工作得很好)。我觉得这可能是因为 NetBeans ClassLoader 的工作方式与 JVM ClassLoader 不同,但我不知道,我当然不是这方面的专家。这是我的主要功能的代码......

////////////////////////////////////////////////////////////////////////////////////////////
package gql;

import java.io.*;
import java.util.*;
import com.db4o.*;

public class GQL {
////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
////////////////////////////////////////////////////////////////////////////
private static ObjectSet dbObjects;
private static LinkedList classes = new LinkedList();
private static String dbPath, classPath;
private static ObjectContainer db;
private static ClassLoader coreClassLoader =
ClassLoader.getSystemClassLoader();
private static ClassLoader subClassLoader =
ClassLoader.getSystemClassLoader();

public static void main(String[] args) {
////////////////////////////////////////////////////////////////////////
// CREATE DATABASE OBJECT
////////////////////////////////////////////////////////////////////////
// If no path to a database is provided on the command line, print
// error and exit program
if (args.length < 1) {
System.err.println("\nError: no database path provided.\n");
return;
} else if (args.length > 1) {
dbPath = args[0];
// TODO - dubug command line classpath
classPath = args[1];
db = Db4o.openFile(dbPath);
} else { // We assume that the database Classes are stored somewhere
dbPath = args[0]; // along the CLASSPATH, and therefore classPath
classPath = ""; // can be left empty
db = Db4o.openFile(dbPath);
}

System.out.print("GQL> ");

// The prompt of the interpreter is within a do-while loop, which can
// be terminated by entering "exit"
do {
try {

/////////////////////////////////////////////////////////////////
// READ IN QUERY FILE
/////////////////////////////////////////////////////////////////
// We create a Scanner object to read tokens from the standard in
// stream - these will be our DLOG files provided by the user
Scanner fileScanner = new Scanner(System.in);
String GQLFile = fileScanner.next();

// Break loop and exit program if user enters "exit"
if (GQLFile.equalsIgnoreCase("exit")) {
break;

// If the user input is not preceeded by "@" and teminated with
// ";" then the input is invalid - the user is prompted again
} else if (!(GQLFile.substring(0,1).equals("@")) ||
!(GQLFile.substring(GQLFile.length()-1,
GQLFile.length()).equals(";"))) {

System.out.println("\nInvalid input.\nUsage: "
+ " @filename;\n");
System.out.print("GQL> ");
continue;

} else {

// Parse out the "@" and ";" from the user's input and send
// this to a file Reader object
GQLFile = GQLFile.substring(1,GQLFile.length()-1);
}

// Now we create a reader object and give it the user's parsed
// input - in the event of a FileNotFoundException, the user is
// prompted again
Reader reader;
try {
reader = new FileReader(GQLFile);
} catch (FileNotFoundException e) {
System.out.println("\nFile " + GQLFile +
" does not exist.\n");
System.out.print("GQL> ");
continue;
}

/////////////////////////////////////////////////////////////////
// PARSE QUERY
/////////////////////////////////////////////////////////////////
// The parser and Lexer objects are created in the parser.java
// and Lexer.java files, respectively - The parser takes the
// Lexer as an argument - the value variable generated by the
// parse() method will return the topmost grammar construct,
// which in this case is a Query object
parser p = new parser(new Lexer(reader));

Query query = (Query) p.parse().value;
/////////////////////////////////////////////////////////////////
System.out.println("\n----------------------------Input Query-----" +
"-----------------------\n");

System.out.println("\n SUCCESSFUL PARSE " +
" \n");

System.out.println("--------------------------------------" +
"------------------------------\n");
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// LOAD ALL CLASSES USED IN DATABASE INTO RUNTIME
/////////////////////////////////////////////////////////////////
// databse Classes should be kept on the CLASSPATH, or the path
// to these classes should be provided as a second command line
// argument
boolean coreClassesLoaded = loadCoreClasses(coreClassLoader,
classPath);
if (!coreClassesLoaded) {
System.err.println("\nError: one or more of core Classes"
+ "Node, Egge and SimplePath could not be found.\n");
db.close();
return;
}
//
System.out.println("Core classes loaded.\n");
boolean subclassesLoaded = query.loadSubclasses(subClassLoader,
classPath);
if (!subclassesLoaded) {
System.err.println("\nError: subclasses could not be" +
" loaded.\n");
db.close();
return;
}
//
System.out.println("Subclasses loaded.\n");


/////////////////////////////////////////////////////////////////
// MAKE SURE THE DATABASE ACTUALLY CONTAINS SOME OBJECTS AND,
// IF SO, PUT AN INSTANCE OF EACH CLASS REPRESENTED INTO THE
// LINKEDLIST CLASSLIST - SINCE WE LOADED THE DATABASE CLASSES
// INTO THE RUNTIME ENVIRONMENT, OBJECTS RETURNED BY DATABASE
// QUERIES WILL REMAIN TRUE TO THEIR TYPE; IF WE HADN'T DONE
// THIS, THESE OBJECTS WOULD BE RETURNED AS TYPE GENERICOBJECT
/////////////////////////////////////////////////////////////////
dbObjects = db.queryByExample(Object.class);
if (dbObjects.hasNext()) {
query.addClassesToList(dbObjects, classes);
} else {
System.err.println("\nError: no objects in database.\n");
db.close();
return;
}
//
System.out.println(classes);

/////////////////////////////////////////////////////////////////
// SEMANTIC CHECKS //
/////////////////////////////////////////////////////////////////
boolean headArgsAreSet = query.setHeadArgClasses(classes);
if (!headArgsAreSet) {
db.close();
return;
}
boolean typesMatch = query.checkQueryTypes(db);
if (!typesMatch) {
db.close();
return;
}

/////////////////////////////////////////////////////////////////
// EVALUATION
/////////////////////////////////////////////////////////////////
query.evaluateQuery(db);

} catch (Exception e) {
System.out.println("\nSYNTAX ERROR\n");
e.printStackTrace();
}

System.out.print("GQL> ");
} while (true);

System.out.println("\nExiting...\n");
db.close();
}

private static boolean loadCoreClasses(ClassLoader coreClassLoader,
String classPath) {
try {
coreClassLoader.loadClass("Node");
coreClassLoader.loadClass("Edge");
coreClassLoader.loadClass("SimplePath");
} catch (ClassNotFoundException cnfe) {
return false;
}
return true;
}
}



////////////////////////////////////////////////////////////////////////////////////////////

奇怪的是,我需要的类肯定会加载到运行时环境中,因为我使用它们来设置一些类成员变量,例如在“语义检查”部分中。因此,就像应用程序可以看到动态加载的类,但 db4o API/数据库却不能。另外,我将 Class jar 和 db4o jar 设置为 Netbeans 库,而不仅仅是在 .../jre/lib/ext 中。这是类中的代码片段,我实际上在其中使用了 db4o native 查询,这给我带来了问题......

////////////////////////////////////////////////////////////////////////////////////////////

public void evaluateQuery(ObjectContainer db) {
if (this.hasPredicate) {
;
} else {
if (this.isNode) {
List nodes = db.query(new Predicate() {
@Override
public boolean match(Node node) {
return (node.getName().equals("5"));
}
});
System.out.println("\n_________________RESULT__________________________");
System.out.println("\nNode: " + nodes.get(0).getName()
//+ ".\n");
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////

...如果我执行以下操作,我仍然得到一个空列表...

////////////////////////////////////////////////////////////////////////////////////////////


public void evaluateQuery(ObjectContainer db) {
if (this.hasPredicate) {
;
} else {
if (this.isNode) {
List nodes = db.queryByExample(Node.class);
System.out.println(nodes.size());
for (int i = 0; i < nodes.size(); i++) {
System.out.println("\nNode: " + nodes.get(i).getName());
}
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////

最佳答案

嗯,我在这里没有看到任何动态类加载问题。您正在使用系统的类加载器,它应该对 db4o 可见。您甚至不需要加载类,db4o 将使用类加载器来完成此操作。

您确定应用程序选择相同的数据库吗?您使用相对路径吗?

顺便说一句,您可以显式设置 classloader for db4o ,像这样。

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

JdkLoader classLookUp = new ClassLoaderLookup(
Thread.currentThread().getContextClassLoader(),
new URLClassLoader(new URL[]{new URL("file://./some/other/location")}));
configuration.common().reflectWith(new JdkReflector(classLookUp));

ObjectContainer container = Db4oEmbedded.openFile(configuration,"database.db4o");

关于java - Db4o API 在 NetBeans 中看不到动态加载的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11496038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com