ClassNotFoundException when extending PApplet

As an experiment i try to extend PApplet and run that.

This is the library class:

public class Problessing extends PApplet {

    static public void main(final String[] args) {
        PApplet.main(args);
    }
}

This is my processing sketch:

import nl.doekewartena.problessing.*;

static final void main(String[] args) {

  final String sketch = Thread.currentThread()
    .getStackTrace()[1].getClassName();

  println(sketch);

  Problessing.main(sketch+".Test");
}



class Test extends Problessing {

  void setup() {
    println("ok");
  }
}

Instead of Problessing.main(sketch+".Test"); I also tried Problessing.main("Test"); but still i get ClassNotFoundException.

Can someone help?

Answers

  • I'm a bit further, i had to use a $ instead of a dot. In processing the class is found now but i get a "java.lang.instantiationException".

    static final void main(String[] args) {
    
      final String sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();  
    
      Problessing.main(sketch+"$Test");
    }
    

    In intelliJ it works now.

    import nl.doekewartena.problessing.Problessing;
    
    public class Test extends Problessing {
    
        public static void main(String args[]) {
    
            Problessing.main("Test");
        }
    
    
        public void setup() {
            println("ok");
        }
    
    }
    

    Someone an idea why processing can't make the instance?

  • Gonna repost what I replied in private here to help context:

    1st, I don't see any logic why any1 would wanna extend PApplet. Not even the other Processing libraries do that! A simple import would suffice IMO! :P

    But if you insist on that... you're gonna need your own static main(String[] args) {}. So you can force a call to Problessing.main("") in place of the original PApplet.main("")!

    Since your Problessing library is actually a hacked fork of the original PApplet,
    both you and any1 trying to use it gotta struggle against Processing's pre-processor!

    The pre-processor wraps up all ".pde" tabs w/ public class Name_of_1st_PDE_Tab extends PApplet {}.
    Therefore, you can't change it to extends Problessing {}! And even Problessing.main("") gonna fail!

    As long Processing's pre-processor is in the way, your project is doomed! :o3

  • edited July 2014 Answer ✓

    The difference between the 2 examples in your last comment is that

    in Processing you are trying to instantiate an inner class

    in IntelliJ you are instantiating a top-level class

    I suggest you make the Test class a top level class by declaring it static and see what happens

    import nl.doekewartena.problessing.*;
    
    public static final void main(String[] args) {
      Problessing.main("Test");
    }
    
    public static class Test extends Problessing {
    
      void setup() {
        println("ok");
      }
    }
    

    EDIT posted before I saw GoToLoop's comment and became aware PMs

  • edited July 2014

    Seems like @quark got ahead of what I was already fomenting as a possible solution.
    Although a heavy boilerplate to ask any regular Processing programmer to paste before using a "library"!

    Below's the general recipe idea:

    // forum.processing.org/two/discussion/6256/
    // classnotfoundexception-when-extending-papplet
    
    static final void main(final String[] args) {
      PApplet.runSketch(append(args, ""), new MyApp());
    }
    
    static final class MyApp extends PApplet {
      void setup() {
        println("OK!");
        exit();
      }
    }
    

    And now the same above adapted to the new fork library:

    // forum.processing.org/two/discussion/6256/
    // classnotfoundexception-when-extending-papplet
    
    import nl.doekewartena.problessing.*;
    
    static final void main(final String[] args) {
      Problessing.runSketch(append(args, ""), new MyApp());
    }
    
    static final class MyApp extends Problessing {
      void setup() {
        println("Hacked!");
        exit();
      }
    }
    

    P.S.: Doing so, we won't be able to use multiple tabs, since they would be PApplet's nested classes!

  • Although a heavy boilerplate to ask any regular Processing programmer to paste before using a "library"!

    I agree but if it just an experiment - it has some interest.

    I suggest that the public access specifier is used - then it is not dependent on the pre-processor doing the job. After all we have no guarantee that the pre-processor functionality won't change.

  • edited July 2014

    Alternative version. Although even more boiler-plate: >-)

    // forum.processing.org/two/discussion/6256/
    // classnotfoundexception-when-extending-papplet
    
    static final void main(final String[] args) {
      final String sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      PApplet.main(append(args, sketch + "$MyApp"));
      //PApplet.runSketch(append(args, ""), new MyApp());
    }
    
    public static final class MyApp extends PApplet {
      void setup() {
        println("OK!");
        exit();
      }
    }
    
  • edited July 2014

    I suggest that the public access specifier is used.

    I'm posting a Processing's IDE oriented recipe for regular programmers. :\">
    I don't wanna make the recipe even more complicated than it already is! (~~)
    After all, Processing's pre-processor already place public in almost anything. :\">
    Any user for other pro IDE's already knows which ".pde" modifications gotta be done anyways! ;))

    P.S.: My latest boiler-plate experiment indeed demands the public inclusion, so reflection works!
    On the other hand, directly instantiating the hacked class and passing that to runSketch() doesn't demand public! :P

  • edited July 2014

    Newer boilerplate version. This time, besides not needing the name for the top-class,
    we don't need for its nested class either when invoking PApplet.main("")! Check it out: \m/

    // forum.processing.org/two/discussion/6256/
    // classnotfoundexception-when-extending-papplet
    
    public static final void main(final String[] args) {
      final String nested, sketch = Thread.currentThread()
        .getStackTrace()[1].getClassName();
    
      try {
        nested = Class.forName(sketch)
          .getClasses()[0].getName();
      }
    
      catch (final ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    
      PApplet.main(append(args, nested));
      //PApplet.runSketch(append(args, ""), new MyApp());
    }
    
    public static final class MyApp extends PApplet {
      void setup() {
        println("OK!");
        exit();
      }
    
      void draw() {
        background(-1);
      }
    }
    
  • Damn, it really sucks that other classes can't be used since they will be nested. Good thing is it still works in eclipse and IntelliJ.

    What i can do now for example is this:

    import nl.doekewartena.problessing.*;
    
    public class Test3 extends Problessing {
    
        public static void main(String args[]) {
            Problessing.main("Test3");
        }
    
        public void setup() {
            size(200, 200, MM); // PDF will be this size in MM
            noiseMode(PERLIN); // choose between VALUE and PERLIN noise
        }
    
        public void draw() {
            background(noise(frameCount*0.02f)*255);
    
            stroke(Red); // all CSS color names are working
            fill(DarkCyan);
    
            strokeWeight(3, MM);
    
            // fmouseX is frameMouseX, this can give negative values
            // and values > width. This way the ellipse won't get stuck at a edge but can leave the frame now
            ellipse(fmouseX, fmouseY, 20, 20);
    
        }
    
    }
    

    Btw, where on github can i find the code of the precompiler? I would like to have a look at it.

Sign In or Register to comment.