I have run the application to load a class name called ChocolateMilk.java inside the netbeans project, I want to call this class using JFileChooser and then load it using ClassLoader.
here is the code for the method to get filename which is inside main class.
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;
}
I am calling getTheFileName method as a parameter for method called run inside foodProcessorEnvironmentA Class.
foodProcessorEnvironmentA.run(getTheFileName());
Here is the full code for the run method.
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.
}
==========================
this is the output after running the project.
=============================
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)
===========
I hope my question is clear. Thank you.