-1

So this is my first time posting, please be gentle :3

General overview:
My goal is to be able to give my program a XSD file, then the program generates a bean, generates a nice GUI using reflection on the bean, then I can enter appropriate values into the textfields and then the program updates the bean with the values and finally marshalls the bean into a nice XML.

It's coming along pretty good, but yesterday I stumbled over an error that I can't figure out..

StackTrace

java.lang.ClassNotFoundException: X.Y.Z.BEAN_FILE_WITHOUT_.JAVA
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at X.Y.Z.GenerateXMLService.generateBean(GenerateXMLService.java:101)
at X.Y.Z.view.Gui$1.handle(Gui.java:56)
at X.Y.Z.view.Gui$1.handle(Gui.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1800(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

generateBean

public Class generateBean(File xsd) {

    String finalDir = System.getProperty("user.dir") + "/src/main/java";
    String xsdDir = xsd.getParentFile().getAbsolutePath();

    try {
        Process p = Runtime.getRuntime().exec(
            "cmd /c cd " + xsdDir + " && xjc -d " + finalDir
                + " -p PACKAGE_PATH_GOES_HERE " + xsd.getName().toString());
        p.waitFor();

    } catch (IOException | InterruptedException e) {
        Gui.errorPopUp(e);
    }

    /**
     * Get the newly created files and put them in an array
     */
    File[] fList = new File[2];
    fList = new File("DIRECTORY_PATH_GOES_HERE").listFiles();

    /**
     * Get the file that isn't Objectfactory.java ---> that's the bean
     */
    for (File file : fList) {
        if (!"ObjectFactory.java".equals(file.getName())) {

            String beanName = file.getName();

            String beanBinaryName = "PACKAGE_PATH_GOES_HERE"
                + beanName.substring(0, beanName.length() - 5);

            Class beanClass = null;
            try {

                beanClass = Class.forName(beanBinaryName); //this is line 101
                return beanClass;

            } catch (ClassNotFoundException e) {
                Gui.errorPopUp(e);
            }

        }
    }
    return null;

}

So I checked first if the beanBinaryName is correct, and I think it is. It has the following format:

com.foo.foo.some.more.foo.beanbag.bean

Then I started googling with the error message and stumbled accross this post:
Link to the blog

But to be honest I didn't really understand what he was trying to explain. The stacktrace looks similar, but I just started with java a few months ago and still consider myself a newbie. Do you have any advice what the error might be? Thank in advance!

EDIT / SOLUTION:

My first mistake was trying to use class.forName() on a .java file. This only works with compiled files, e.g. .class.

After I fixed that i was still getting error messages, but after playing around with the URLClassLoader it worked :D I renamed and refactored this method compared to the method at the top, so in this one we really only get the class, while in my first example I also generate the .java files. I now do this in a seperate method to keep things organized.

public Class getClassFromBean() {

    File[] fList = new File[3];
    fList = new File("foo/beanbag").listFiles();
    Class beanClass = null;

    for (File file : fList) {

        String lastSix = file.getName().toString().substring(file.getName().length() - 6);

        if (".class".equals(lastSix)) {

            String beanBinaryName = "foo.beanbag."
                + file.getName().toString().substring(0, file.getName().toString().length() - 6);

            try {
                File root = new File(System.getProperty("user.dir") + "/src/main/java");
                URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI()
                    .toURL() });
                beanClass = Class.forName(beanBinaryName, true, classLoader);
            } catch (ClassNotFoundException | MalformedURLException e) {
                Gui.errorPopUp(e);
            }

            return beanClass;

        }

    }
    return beanClass;

}
anon
  • 3
  • 3
  • Are you generating '.class' file? – anstarovoyt Nov 28 '14 at 16:00
  • Check that ch.bedag.ste.lehrling.aze.reflection.beanbag.Durchstich in class path – talex Nov 28 '14 at 16:08
  • @anstarovoyt Do you mean with the command line (Process p)? This gives me two files, BEAN_THAT_I_WANT.java and Objectfactory.java. – anon Nov 28 '14 at 16:16
  • @talex Hmmmm.. I actually didn't want you to see that part^^ Where did you see that? And what do you mean with check? Does it look wrong? – anon Nov 28 '14 at 16:17
  • @anon It is magic :). Actually it was in you post before edit. No it is looks ok. Check that class with this name in your classpath – talex Nov 28 '14 at 16:19
  • @talex Haha magic okay :P From now on i will finish my questions in one setting – anon Nov 30 '14 at 17:41

1 Answers1

1

Method Class.forName() works only for compiled classes i.e. for files with extension '.class'. You cannot just put '.java' file to your classpath.

anstarovoyt
  • 7,956
  • 2
  • 27
  • 35
  • Hmm okay.. So if I use the file, then compile it using _javac_ and continue it should work? – anon Nov 30 '14 at 19:58
  • @anon I am not sure because it depends on classloading implementation & etc. You can look on this question http://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class – anstarovoyt Nov 30 '14 at 20:28
  • Sorry it took so long for me to update this question. I don't really have a good excuse.. Better late than never so I wanted to thank you for pointing me in the right direction with your link. :) – anon Dec 09 '14 at 09:49