ClassNotFoundException on running main (urgent)

guys please help, I need to finish something on Monday for my exam on Tuesday! I didn't start to late, i have worked 7 days a week for so many months now :( My whole project is just a lot of work.

This is related to the program Max, however, i don't think any knowledge of max is required.

I have a class, which extends another class, Max needs to use this class. From this class i want to create a PApplet. Creating the Object goes fine, however it goes wrong with calling the main method of the PApplet. It gives a ClassNotFoundException, i have the full error log below.

Please tell me what i can do / try. Else i have to do everything with OSC which would make things very complicated.

public class HelloWorld extends MaxObject {

    public static class Test {

        Test() {
            // this is to have a message in Max console
            post("construct Test");

            // create the Processing sketch
            MaxTest_01 mt_01 = new MaxTest_01();
            // call main
            // here is where it goes wrong
            mt_01.main(new String[]{});

        }

    }

    public HelloWorld () {

        // this works
        /*
        JFrame frame = new JFrame("FrameDemo");
        frame.pack();
        frame.setVisible(true);
         */
        Test t = new Test();
    }

}

MaxTest_01:

public class MaxTest_01 extends PApplet {

    public static void main(String args[]) {
        // this goes wrong:
        PApplet.main(new String[]{"--display=0", "MaxTest_01"});
    }

    public void setup() {
    }

    public void draw() {
    }
}


java.lang.RuntimeException: java.lang.ClassNotFoundException: MaxTest_01
    at processing.core.PApplet.runSketch(PApplet.java:10713)
    at processing.core.PApplet.main(PApplet.java:10525)
    at MaxTest_01.main(MaxTest_01.java:10)
    at HelloWorld$Test.<init>(HelloWorld.java:36)
    at HelloWorld.<init>(HelloWorld.java:52)
Caused by: java.lang.ClassNotFoundException: MaxTest_01
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at processing.core.PApplet.runSketch(PApplet.java:10710)
    ... 4 more
(mxj) unable to construct instance of HelloWorld

Answers

  • The above still counts, however i tried a different approach. My draw is running now but i don't see the PApplet.

    public class PAppletInMax extends MaxObject {
    
        public PAppletInMax() {
            MaxTest_01 sketch = new MaxTest_01();
            sketch.init();
        }
    
    }
    

    the PApplet:

    public class MaxTest_01 extends PApplet {
    
        public void setup() {
            size(500, 500);
        }
    
        public void draw() {
            MaxObject.post("draw");
        }
    
    }
    

    please help

  • edited June 2014

    1st of all, public static final void main(String[]){} has a special meaning to Java!
    This is the entry point where a Java program starts!

    public class MaxTest extends PApplet {
    
      public static final void main(String[] args) {
        final String sketch = Thread.currentThread()
          .getStackTrace()[1].getClassName();
    
        PApplet.main(concatArgs(sketch, args, "--display=0"));
      }
    
      static final String
        concatArgs(String name, String[] oldArgs, String... newArgs)[] {
        return append(concat(newArgs, oldArgs), name);
      }
    
      void setup() {
        size(800, 600, JAVA2D);
        noLoop();
        background(0);
      }
    
      void draw() {
      }
    }
    

    If the class MaxTest isn't your app's entry point, I suggest you to change its main() method's signature!

    2nd thing is that if a method is static, invoking it using an object reference doesn't change that!

    For example your line #13 @ HelloWorld: mt_01.main(new String[]{});.
    Its effect is the same as: MaxTest_01.main(new String[]{});!

    3rd, PApplet.main() instantiates a brand new PApplet by reflection, but doesn't return its reference.
    That's where your statement mt_01.main(new String[]{}); got it all wrong!
    The mt_01's reference value doesn't count at all! It's a static call after all!

    But there's still hope. There's another PApplet.main()'s variant called PApplet.runSketch()!
    Actually, PApplet.runSketch() is always called by PApplet.main().
    And it's the real thing that does all the instantiation & initialization work!

    Take a look at its source code:
    https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L10555

    And parameters:
    static public void runSketch(final String args[], final PApplet constructedApplet) {

    @ line https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L10705
    we can see that constructedApplet reference is assigned to final PApplet applet;.

    Otherwise, when constructedApplet is passed as null, it instantiates a new PApplet by reflexion
    according to variable name, which comes from args[] after being previously parsed.

    In short, we can instantiate a PApplet by ourselves and directly pass its reference to PApplet.runSketch()!

    Check out an example I've got from:
    http://forum.processing.org/two/discussion/5840/eclipse-multiple-frames-avoid-null-frame-value-

    static final void
    main(String[] args) {
      PApplet.main(concatArgs("RotatingCard", args
        , "--location=50,50"));
    
      PApplet.main(concatArgs("RotatingCars", args
        , "--location=600,300", "--display=1"));
    
      //PApplet.main(concatArgs("SquareFlower", args
      //, "--location=1300,50"));
    
      PApplet.runSketch(concatArgs("", args
        , "--location=1300,50"), new SquareFlower());
    }
    
    static final String[]
    concatArgs(String name, String[] oldArgs, String... newArgs) {
      return append(concat(newArgs, oldArgs), name);
    }
    

    PApplet.runSketch() was alternatively used for SquareFlower initialization rather than PApplet.main()! =:)

  • Thanks for the long answer :) I start reading now.

    public static final void main(String[] args) { doesn't get called by Max, it starts java things a bit different so there is no need to have it there.

    If the class MaxTest isn't your app's entry point, I suggest you to change its main() method's signature!

    What do you mean?

    This is one of my 2 new attempts:

    public class PAppletInMax extends MaxObject {
    
        public PAppletInMax() {
            PApplet.runSketch(new String[] { "" }, new MaxTest_01());
        }
    
    }
    

    I don't have any args cause it's not a PApplet class. If i try the above in Max then max freezes so it's a bit hard to tell what goes wrong.

    I did a big investigation, i will post it in a new comment.

  • hmm, my body is 20922 characters to long so i forget about dropping the code now. I copy pasted the runSketch so i could debug it in max. It seemed it would always crash when runSketch was calling init() (if i remember correct).

    Also this was a problem (it's slightly changed i guess):

     if ((size.width != 0) && (size.height != 0)) {
                // When this PApplet is embedded inside a Java application with other
                // Component objects, its size() may already be set externally (perhaps
                // by a LayoutManager). In this case, honor that size as the default.
                // Size of the component is set, just create a renderer.
                p.g = makeGraphics(size.width, size.height, p.sketchRenderer(), null, true, p);
                // This doesn't call setSize() or setPreferredSize() because the fact
                // that a size was already set means that someone is already doing it.
    
            } else {
                // Set the default size, until the user specifies otherwise
                // clankill3r:
                //this.defaultSize = true;
                p.defaultSize = true;
                int w = p.sketchWidth();
                int h = p.sketchHeight();
                p.g = makeGraphics(w, h, p.sketchRenderer(), null, true, p);
                // Fire component resize event
                p.setSize(w, h);
                p.setPreferredSize(new Dimension(w, h));
            }
    

    I have around 900 lines of code now, processing has a lot of protected stuff which is really bugging me lately, it makes some things really complicated cause it requires to copy so much code from processing, and where it starts with one methods it keeps going on. And there are always dead ends. When i have more time i will download the processing source to see if i can make a runSketch method that will work for Max. I think a lot of people would really like that. All the answers i see in the max forums of how to get simple data from java, they tell to use OSC. Being able to avoid OSC and use processing would be a big leap (but also with some down sides, the project would have to launch from max since the JVM is started there).

  • O yeah and the problem with this:

    MaxTest_01 sketch = new MaxTest_01();
     sketch.init();
    

    Is that frame is null (and all other things that happen in runSketch).

  • Have you tried running my big 3 apps in 1 from the link below already?:
    http://forum.processing.org/two/discussion/5840/eclipse-multiple-frames-avoid-null-frame-value-

    Perhaps the solution is much simpler than we think!
    My idea is define a constructor for the PApplet classes.
    Inside the constructor, call PApplet.runSketch()! *-:)

  • If the class MaxTest isn't your app's entry point, I suggest you to change its main() method's signature! <- What do you mean?

    A Java program gotta start from somewhere, don't ya think?
    Place public static final void main(String[]){} inside the class which should ignite the whole app! 8-X

  • edited June 2014

    Been doing some crazy experiments here...
    Made a class which extends PVector which in turn instantiates another extending PApplet.

    The former is instantiated by a class called PApplet_Class_Igniter,
    where static final void main(String[] args) {} is placed!

    Check them all out below:
    http://studio.processingtogether.com/sp/pad/export/ro.90WsCogc75rxf/latest

    "PApplet_Class_Igniter.pde":


    /**
     * PApplet Class Igniter (v1.02)
     * by GoToLoop (2014/Jun)
     *
     * forum.processing.org/two/discussion/6025/
     * classnotfoundexception-on-running-main-urgent
     *
     * forum.processing.org/two/discussion/5840/
     * eclipse-multiple-frames-avoid-null-frame-value-
     */
    
    static final void main(String[] args) {
      new PVecPApp(args);
    }
    


    "PVecPApp.java":


    import processing.core.PApplet;
    import static processing.core.PApplet.*;
    
    import processing.core.PVector;
    
    public class PVecPApp extends PVector { // in place of MaxObject.
      final PApplet p;
    
      static final String[]
        concatArgs(String name, String[] oldArgs, String... newArgs) {
        return append(concat(newArgs, oldArgs), name);
      }
    
      PVecPApp(String... args) {
        p = new MyPApp(concatArgs("", args
          , "--location=1100,50", "--display=1"));
    
        p.registerMethod("draw", this);
      }
    
      public void draw() {
        final float t = .1f * THIRD_PI * p.frameCount;
    
        for (int n = p.frameCount*3; n-- != 1;) {
          float r = .25f * MyPApp.GAP * sqrt(n);
          float theta = n * MyPApp.ANG;
          float pulse = 6f + MyPApp.RAD*sin(t - n*MyPApp.EIGHTH_PI);
    
          p.fill(0100, map(r*2f, 0, p.width, 0, 0400), 0220, 0200);
          p.ellipse(r*cos(theta), r*sin(theta), pulse, pulse);
        }
      }
    }
    


    "MyPApp.java":


    /**
     * Pulsating Spiral Flower (v3.01)
     * by  Kevin (2014/Apr)
     * mod GoToLoop
     *
     * forum.processing.org/two/discussion/4266/
     * how-to-create-a-pulsating-particle-system-that-is-being-built
     *
     * studio.processingtogether.com/sp/pad/export/ro.90WsCogc75rxf/latest
     */
    
    import processing.core.PApplet;
    
    public class MyPApp extends PApplet {
      static final int GAP = 020, RAD = 3;
      static final float EIGHTH_PI = QUARTER_PI/2f;
    
      static final float ANG = PI*(3f - sqrt(5f));
    
      MyPApp(String... args) {
        PApplet.runSketch(args, this);
      }
    
      @ Override public void setup() {
        size(500, 500, JAVA2D);
        frameRate(60);
        smooth(4);
        noStroke();
        ellipseMode(CENTER);
      }
    
      @ Override public void draw() {
        background(-1);
        translate(width>>1, height>>1);
      }
    }
    

  • That looks cool. I have no time for a big answer :( I will do that later.

    Thing is i keep running into problems so I go for a simple solution now.

Sign In or Register to comment.