我已运行应用程序在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 Class中运行方法的参数调用。
foodProcessorEnvironmentA.run(getTheFileName());
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)
===========
我希望我的问题很明确。 谢谢。
答案 0 :(得分:2)
您必须以与包括包的完全限定类名相对应的形式向Class.forName()
提供文件名。在这种情况下,foodprocessor.ChocolateMilk.
您需要知道包结构的头部在文件系统中的位置;将其用作提供给URLClassLoader;
的网址,然后调用loadClass("foodprocessor.ChocolateMilk")
。
答案 1 :(得分:0)
我通过将包名称的字符串与dot和programName变量串联来解决问题。
这里
prgramClass = classLoader.loadClass("foodprocessor."+programName);
就是这样!