gpt4 book ai didi

java.lang.NoClassDefFoundError : (wrong name )

转载 作者:行者123 更新时间:2023-12-02 04:50:39 24 4
gpt4 key购买 nike

我已经运行应用程序来加载 netbeans 项目中名为 ChocolateMilk.java 的类,我想使用 JFileChooser 调用该类,然后使用 ClassLoader 加载它。

这是主类中获取文件名的方法的代码。

public static String getTheFileName() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
System.err.println("Error " + ex.getMessage());
}
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(fc.FILES_ONLY);
jfc.setCurrentDirectory(new File(System.getProperty("user.dir") + File.separator + "build"+ File.separator + "classes"));
jfc.showOpenDialog(null);
File myfile ;

String fileNameWithOutExt = jfc.getName((myfile = jfc.getSelectedFile()));
int pos = fileNameWithOutExt.lastIndexOf(".");
if (pos > 0) {
fileNameWithOutExt = fileNameWithOutExt.substring(0, pos);
}
FilenameUtils.removeExtension(jfc.getName((myfile = jfc.getSelectedFile())));

System.out.println("the program name is " +fileNameWithOutExt);
return fileNameWithOutExt;
}

我正在调用 getTheFileName 方法作为 foodProcessorEnvironmentA 类中调用的 run 方法的参数。

foodProcessorEnvironmentA.run(getTheFileName());

这是 run 方法的完整代码。

public void run(String programName) {
URLClassLoader classLoader ;
// the class path is a string that inside foodProcessorEnvironmentA class as URL instance variable


classLoader = new URLClassLoader(classPath);
Class prgramClass = null;

try {
prgramClass = classLoader.loadClass(programName);
} catch (ClassNotFoundException ex) {
Logger.getLogger(FoodProcessorEnvironmentA.class.getName()).log(Level.SEVERE, null, ex);
}
AbstractFoodProcessorProgram program = null;
try {
program = (AbstractFoodProcessorProgram) prgramClass.newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(FoodProcessorEnvironmentA.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(FoodProcessorEnvironmentA.class.getName()).log(Level.SEVERE, null, ex);
}

program.setEnvironment(this);

program.start();

// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

============================

这是运行项目后的输出。

==============================

The fileName is: file:/Users/****/Desktop/FoodProcessor/build/classes/foodprocessor/
the program name is ChocolateMilk
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: ChocolateMilk (wrong name: foodprocessor/ChocolateMilk)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at foodprocessor.FoodProcessorEnvironmentA.run(FoodProcessorEnvironmentA.java:31)
at foodprocessor.FoodProcessor$1.run(FoodProcessor.java:191)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

==========

我希望我的问题很清楚。谢谢。

最佳答案

您必须以与包含包的完全限定类名相对应的形式向 Class.forName() 提供文件名。在本例中,foodprocessor.ChocolateMilk。 您需要知道包结构的头部在文件系统中的位置;使用它作为提供给 URLClassLoader; 的 URL,然后调用 loadClass("foodprocessor.ChocolateMilk")

关于java.lang.NoClassDefFoundError : (wrong name ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267613/

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