LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2004, 12:53 PM   #1
lmellen
Member
 
Registered: Jan 2004
Location: Fairport, NY
Distribution: Arch/Ubuntu
Posts: 43

Rep: Reputation: 15
newbie java question


I'm taking a java course this fall at the local community college.
I thought I'd get a jump on the course so I bought " The Java Tutorial - Third Edition,recommended by Amazon.com.
The book is for j2sdk1.3.1. I downloaded j2sdk1.4.2. Will the newer compiler cause program errors when I do the exercises? I copied the code exactly, javac is ok, creates the class file, but when I try java I get:"Execption in thread "main" java.lang.NoSuchMethodError: main
Thanks -- Larry
 
Old 06-30-2004, 01:00 PM   #2
meblost
Member
 
Registered: May 2004
Location: At Keyboard
Distribution: Mandrake 10.0, SuSE 9.0
Posts: 114

Rep: Reputation: 15
Is there a main method in the file you are compiling? What command are you executing to compile?
 
Old 06-30-2004, 02:19 PM   #3
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
What you need to do is make sure that you have a

Code:
public static void main(String [] args)
method somewhere in the class which you are executing. This is where your program will start when you do "java mainClass" or whatever your class is called.
 
Old 06-30-2004, 03:21 PM   #4
lmellen
Member
 
Registered: Jan 2004
Location: Fairport, NY
Distribution: Arch/Ubuntu
Posts: 43

Original Poster
Rep: Reputation: 15
This is the code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ClickMe extends Applet implements MouseListener {
private Spot spot = null;
private static final int RADIUS = 7;

public void init() {
addMouseListener(this);
}

public void paint(Graphics g) {
//draw a black border and a white background
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

//draw the spot
g.setColor(Color.red);
if (spot != null) {
g.fillOval(spot.x - RADIUS, spot.y - RADIUS, RADIUS * 2, RADIUS * 2);
}
}
public void mousePressed(MouseEvent event) {
if (spot == null) {
spot = new Spot(RADIUS);
}
spot.x = event.getX();
spot.y = event.getY();
repaint();
}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}

It's the same on the tutorial cd that came with the book, it's the same at their website(it's a sun microsystems book.
I don't think they want me to start rewriting code already, but
as dave_starsky stated; also clearly stated in the book:

public static void main(String [] args)

must be used to start all applications.
I can place this above the 4th line of code, if it works.

thanks for the reply -- Larry
 
Old 06-30-2004, 04:06 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
main method is needed for applications entry classes, while your class is an applet, so doesn't need a main.
 
Old 06-30-2004, 04:13 PM   #6
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
You've written an applet, you don't need a main to start it. You need to embed it in an html page to view it (either through the browser or the appletviewer).

Compile your code (javac), but obviously don't try to run it.
Then create an html file like this (change the applet attributes though)
Code:
<html>
<body>
  <applet code="MyJava.class" width="300" height="200">
  </applet>
</body>
</html>
Then run it either through the browser (open the html file) or the appletviewer:
Code:
appletviewer MyJavaHtml.html
Also, you should use JApplet instead of Applet now.
 
Old 06-30-2004, 04:34 PM   #7
lmellen
Member
 
Registered: Jan 2004
Location: Fairport, NY
Distribution: Arch/Ubuntu
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks coolman0stress, that worked. It was also in the book. I'm trying to do 15 things at once. I should have reviewed better. My fault. Thanks for the reply.
Larry
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question on java coolguy_iiit Programming 6 05-12-2005 12:47 PM
newbie java swing question... window size true_atlantis Programming 1 10-07-2004 10:17 AM
A very basic newbie question on Java lmellen Programming 5 07-19-2004 03:59 PM
Java Newbie Question patpawlowski Programming 6 02-27-2004 04:25 PM
Newbie question about executing java command oldoldcat Linux - General 4 01-30-2004 01:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration