File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes class-file not executing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class-file not executing" Watch "class-file not executing" New topic
Author

class-file not executing

I.K. Ben Het
Greenhorn

Joined: Feb 14, 2007
Posts: 2
First of all I want to say I'm a complete noob to Java programming. I have bought a Java Book called "Head First Java, Second Edition" which is refering to this site. This book discusses Java 1.5.
I'm using Java SDK 6 Standard Edition on Windows XP.
No I started with the first code example:



With that said let's tell my problem.

I can compile my "Program" using the standard java compiler, I entered:
javac Party.java

No errors are shown.
After that I type:
java Party

Then the following error is shown on the commandline:
Exception in thread "main" java.lang.NoSuchMethodError: main

According to the Sun-website there must be a main method to start with. So I modified my code to the following:



Compiling goes OK. Then when a run the code nothing happens!?
Could anyone explain what I need to do?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

I think the code you're looking at is an incomplete example -- probably there to demonstrate concepts, but not run as is.

You're right about the main method. You need that as an entry point when running a Java program from the command line. There are just a few more lines needed to get this to display. First, you need to add the Panel to the Frame (in the same way you added the Label to the Panel). f.add(p);

Next, you need to "pack" the frame by calling f.pack(); If you don't do this, the frame's title bar will show, but its content will not show unless you resize the frame.

Finally, you need to make the frame visible by calling f.setVisible(true);.

Adding these 3 lines will display your frame as expected. However, when you try to close the frame, nothing will happen, because you need to define this operation too. I'm not familiar enough with the old awt Frame to know how to do this without extending the WindowAdapter class and overridding a method. So for now, I suggest you just kill the program with Ctrl+C at the Command Prompt.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Well a few quick things that are not related to why nothing is showing up:

The event package is never used.
The variables b, c, and f are never used.

As for why nothing happens, that's not really the case. The code runs, it just isn't designed to do anything.

In your code this is what you are doing:

Frame f=new Frame(); // creating a new frame
Label l=new Label("Party at Tim's"); // creating a new label
Button b=new Button("You bet"); // creating a new button
Button c=new Button("Shoot me"); // creating a new button
Panel p=new Panel(); // creating a new panel
p.add(l); // adding the lable to the panel


If you were to put system.out's between each line, you would see that it's processing your code, but that your code doesn't have any output. You have to give the frame a size and make it visible. If you just want to try out something to see how it works, here is an example


import java.awt.*;
import java.awt.event.*;

// a class that gets all the methods and variables from the Frame class
public class Party3 extends Frame {

// a constructor to create an instance of the class Party3
public Party3() {
//Creates a frame with the title BasicApplication Title
super("BasicApplication Title"); // sets the title

// sets the size of the frame
setSize(200, 200); // sets the size

// creates a label with the word's Hello world placed in the center
Label l = new Label("hello world",Label.CENTER);

// adds the label to the frame in the center of the frame
add(l,BorderLayout.CENTER);

// tells the application to close when you press the x button
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false); dispose();
System.exit(0);
}
});
}

// the main method so that it runs
public static void main(String[] args) {

//creates an instance of the class you created above
Party3 app = new Party3();
app.setVisible(true);
}
}


Hope this helps. More importantly, go to the swing section in the book. They explain how Gui's work there. Swing is like AWT but better
[ February 14, 2007: Message edited by: Nicholas Carrier ]

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
I.K. Ben Het
Greenhorn

Joined: Feb 14, 2007
Posts: 2
I wil try your suggestions. Thanks for clearing things up.
As I just started programming Java I didn't know the code was incomplete as it looked complete for me. I have basic Borland C++ (commandline apps) skills so I think a little that way.

Am I right if I say that the code doe the following:
- create a frame.
- create a label containing a text.
- create two (useless) buttons with their text.
- create a panel.
- Add the label to the panel.

And this needs to be done:
- assign actions to the buttons.
- add the buttons to the panel.
- add the panel to the frame.
- create a titlebar for the frame.
- declare the functions of the standardcontrols (minimize _, full screen [] and close X) at least the cllosing function.
- make it all visible.

Both thanks of your help. I'm happy this is not a version related error as i use the Java SDK 6 and the book is Java 5.
I think it will be better if I don't skip to the Swing chapter as my Java knowledge is as good as none. The book expects that the reader already knows previous chapters.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by I.K. Ben Het:
...Am I right if I say that the code doe the following:
- create a frame.
- create a label containing a text.
- create two (useless) buttons with their text.
- create a panel.
- Add the label to the panel.

And this needs to be done:
- assign actions to the buttons.
- add the buttons to the panel.
- add the panel to the frame.
- create a titlebar for the frame.
- declare the functions of the standardcontrols (minimize _, full screen [] and close X) at least the cllosing function.
- make it all visible...

Yes, I think your lists show a good understanding of what the code does, and what it does not do.

Assigning actions to the buttons is slightly more involved, because you would need to create and assign Listeners that would respond to events (like a click). The maximize and minimize buttons should work without further coding -- it's just the close button that needs attention. (Note: In some cases, you actually want the program to exit when the close button is clicked. But in other cases, you might want to dispose of the window while keeping the program running. Or you might want to just hide the window.)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by I.K. Ben Het:
...I'm happy this is not a version related error as i use the Java SDK 6 and the book is Java 5...

I expect you'll be just fine using Java 6 with this book. The changes aren't that significant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class-file not executing