Wednesday, February 4, 2009

JCreator Runtime Woes

Ok. So we had some problems getting some main application code to run in JCreator today. My bad. I'd blithely assumed that JCreator would see our main() method in whatever file it was in and automagically run the right thing. Ah....not so much.

Here's the basic task (revised).

  1. Create a new Basic Java Application called Circle and enter the text that's on page 181 in the text.
  2. Keep the existing main() method template that's automatically filled in for you. The code you add will be under public class Circle { but outside the main() method declaration (public static void main(String[] args){ blah blah blah }
  3. Declare a new Circle object in main()
  4. Print out it's radius
  5. Change it's radius
  6. Print out the radius again.
If you create the main() method in another file you need to tell JCreator that Circle.java no longer is supposed to contain your main() method.
  1. Create a new File by going to the Filemenu and selecting New and then File.
  2. The File Wizard dialog box pops up. Under Java Classes, select Main Class.
  3. Give your Main Class file a name.
  4. Add in your code to declare the new circle object and change and display its radius, etc.
  5. Go to the Project menu
  6. Select Project Settings
  7. The Project Settings dialog box pops up. Select your new Main Class file from the Run drop down list and click OK. (You've just told Java that this is the file it's supposed to use to run your project.)
If you haven't done this or something like it (there's more than one way to skin this (and most) cats) you will get the error a bunch of you saw today:
java.lang.NosuchMethodError: main
Exception in thread "main"
Process completed.

If you see this then you almost certainly need to tell JCreator where to look for the main()( method...Or you haven't specified one and are trying to run it.

2 comments: