We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentCore,  Processing Development Environment (PDE) › overwriting main([]), "bug or feature"
Page Index Toggle Pages: 1
overwriting main([]), "bug or feature"? (Read 2623 times)
overwriting main([]), "bug or feature"?
Jan 26th, 2010, 3:15am
 
Hey!

Today I noticed that main can't be overwritten from within the PDE.

Executing this code...
Code:

static public void main(String args[]) {
   println( "in main" );
   PApplet.main(new String[] { "class..." } );
}


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


...will only output "in setup".
However... it does get called just as expected when exporting an application.

Now I'm curious because I've been using Processing from within eclipse for the longest time --- is this the expected behaviour, a bug, or am i doing something wrong?

I find it slightly confusing that it works when exported, but not from within the PDE.
Re: overwriting main([]), "bug or feature"?
Reply #1 - Jan 26th, 2010, 5:16am
 
Yes, it shouldn't work...

When you are writing in processing, you write a code and finally, you get a *.pde file (or a set of *.pde files). This code is almost a JAVA file, but some operations need to be done on it to translate it to a proper JAVA code. This is done behind through a pre compiler. I'm looking for the right place where this happen, but I can't find exactly. (http://dev.processing.org/source/index.cgi/trunk/processing/app/src/processing/app/)

Let me try to explain basically what the precompiler is doing:
(* correct me if I'm wrong)

  • Merge all *.pde files in one (sequential)
  • Add f to floats types
  • Colors types are replaced by ints (*I'm not sure of that one).
  • Create a giant class where everything you had is now inner class.
  • Move the imports statements outside of the general class.
  • Handle public|private statements (for setup(), main(), keyPressed()...)
  • Add a main method, which is your entry point in your sketch


You then have a JAVA code. This is also why it seems to be problematic for the parametrized types such ArrayList<Type>, where the precompiler doesn't know what to do.

Then, if try to override public main(String[] args){...}, you get a conflict, because the precompiler is also adding a main(String[] args). I don't know about your specific problem, but can you just use setup instead, or you really need to do something before?

By the way, what you cant do, is to create a new tab named something.java. The .java extension is important. This file will be treated just as a normal JAVA file (no modification will be made to this one). This tab will be saved and compiled just as in Eclipse, but you need to write JAVA code (as you are used to in Eclipse).

I hope it may help.
Re: overwriting main([]), "bug or feature"?
Reply #2 - Jan 26th, 2010, 5:28am
 
gll wrote on Jan 26th, 2010, 5:16am:
Yes, it shouldn't work...

Let me try to explain basically what the precompiler is doing:
(* correct me if I'm wrong)

  • [...]
  • Add a main method, which is your entry point in your sketch



I think everything you say is correct, except the last point.
I can't imagine that ever happening. There would be two methods then (the one i wrote and the one added), which the compiler wouldn't enjoy.
Also this wouldn't explain why it works when I export the app (that tells me my custom main made it into the class).
It seems it just doesn't get used when run from within the PDE.

It might be that when the sketch is launched from the PDE they use something like
java -classpath=... processing.core.PApplet <my-class>
instead of
java -classpath=... <myclass> <myclass>


I'm still curious if this happens on purpose.
At least it seems a bit incosistent if it works in exports and not in the PDE.

best,
h,-
Re: overwriting main([]), "bug or feature"?
Reply #3 - Jan 26th, 2010, 5:47am
 
gll is right on all points...
I would add/change:
  • Wrap code in setup() if there is no such function.
  • Add a main method, which is your entry point in your sketch, if there is no such function.

Ie. the changes can be conditional...
In other words, your main() method doesn't override (in the Java sense) Processing's one, but it replaces it.

BTW, gll, that's Java, not JAVA, that's not an acronym.
Re: overwriting main([]), "bug or feature"?
Reply #4 - Jan 26th, 2010, 5:47am
 
You are right, main should be added only when it's not there.
You may have a look at the source code. In the processing application itself (not the core librairies). http://dev.processing.org/source/index.cgi/trunk/processing/app/src/processing/app/

I can't find where it is exactly...
Re: overwriting main([]), "bug or feature"?
Reply #5 - Jan 26th, 2010, 6:04am
 
I thought that J.A.V.A. was standing for Jamaica Association of Villas and Apartments. Now, I'm completely lost...

... or, as someone pointed out during a workshop (Java™) should be the correct way, isn't?  Wink
Re: overwriting main([]), "bug or feature"?
Reply #6 - Jan 26th, 2010, 6:08am
 
Ah, i found it in http://dev.processing.org/source/index.cgi/trunk/processing/app/src/processing/app/debug/Runner.java?view=markup


Code:

protected String[] getSketchParams() {
ArrayList<String> params = new ArrayList<String>();

params.add("processing.core.PApplet");
[...]
params.add(appletClassName);


Hm... seems it was there as soon as Runner.java was introduced (2008), which means if this ever worked then a very long time ago...

I filed a bug anyways, should be a minor change:
http://dev.processing.org/bugs/show_bug.cgi?id=1446
Re: overwriting main([]), "bug or feature"?
Reply #7 - Jan 26th, 2010, 6:11am
 
gll wrote on Jan 26th, 2010, 6:04am:
I ... or, as someone pointed out during a workshop (Java™) should be the correct way, isn't  Wink


you took a workshops on how to write JaVa correctly
Re: overwriting main([]), "bug or feature"?
Reply #8 - Jan 26th, 2010, 6:23am
 
You CaPsLock PoNks....

We were giving a workshop on Processing and we were overwhelmed|enthusiast about the open-sourceness of the approach until someone came with the fact that jAvA was (at that time) more opaque than it seemed.
Page Index Toggle Pages: 1