We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
the PApplet:
please help
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!
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 reflexionaccording 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-
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.What do you mean?
This is one of my 2 new attempts:
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):
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:
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()! *-:)
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-XBeen 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":
"PVecPApp.java":
"MyPApp.java":
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.