"No main classes found" - can't compile anything in Java Netbeans 7.1
11 replies, posted
I just started Java as part of my Engineering Degree at uni. One of my first (and rather simple) projects is to create this picture using java.awt. Whenever I compile it it get the message: "No main classes found"
Can anyone help me here? I would normally go in a ask a lecturer but it's a public holiday so everyone is at home
Can you provide us the code your using?
[code]//<editor-fold defaultstate="collapsed" desc="Package Header">
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab1_4;
//</editor-fold>
import java.awt.*;
import javax.swing.JApplet;
//<editor-fold defaultstate="collapsed" desc="Class Declaration">
/**
*
* @author COMP1102
*/
public class Lab1_4 extends JApplet {
/**
* Initialization method that will be called after the applet is loaded into
* the browser.
*/
public void init() {
// TODO start asynchronous download of heavy resources
setSize(400,300);
}
// TODO overwrite start(), stop() and destroy() methods
//</editor-fold>
public void paint(Graphics page) {
//Add your code here
page.setColor (Color.red); //Red, top left corner
page.fillRect (0, 0, 200, 150);
page.setColor (Color.blue); //Blue, top right corner
page.fillRect (200, 0, 200, 150);
page.setColor (Color.yellow); //Yellow, bottom left corner
page.fillRect (0, 150, 200, 150);
page.setColor (Color.green); //Green, bottom right corner
page.fillRect (200, 150, 200, 150);
page.drawLine (0, 150, 200, 0); //Diagonal lines
page.drawLine (200, 0, 400, 150);
page.drawLine (0, 150, 200, 300);
page.drawLine (200, 300, 400, 150);
page.setColor (Color.yellow); //Yellow part of centre circle
page.fillArc (200, 150, 100, 100, 0, 90);
page.drawArc (200, 150, 100, 100, 90, 180); //Uncoloured part of centre circle (red sector)
page.setColor (Color.blue); //Blue part of centre circle
page.fillArc (200, 150, 100, 100, 180, 270);
page.drawArc (200, 150, 100, 100, 270, 360); //Uncoloured part of circle (green sector)
}
}
[/code]
We're supposed to be recreating this:
[img]http://i.imgur.com/sHu7L.png[/img]
[code]public static void main(String [ ] args)
{
//Code Here
}
[/code]
Your missing this.
Read up on the main method here:
[url]http://docs.oracle.com/javase/tutorial/getStarted/application/index.html[/url]
[QUOTE=Doritos_Man;35102289][code]public static void main(String [ ] args)
{
//Code Here
}
[/code]
Your missing this.
Read up on the main method here:
[url]http://docs.oracle.com/javase/tutorial/getStarted/application/index.html[/url][/QUOTE]
I thought this:
[code]public void paint(Graphics page) {
//Add your code here
}[/code]
Did that
[QUOTE=download;35102311]I thought this:
[code]public void paint(Graphics page) {
//Add your code here
}[/code]
Did that[/QUOTE]
Every application in Java needs a main method.
It runs everything in Main, without it Java doesn't know where to start.
[editline]11th March 2012[/editline]
[code]//<editor-fold defaultstate="collapsed" desc="Package Header">
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab1_4;
//</editor-fold>
import java.awt.*;
import javax.swing.JApplet;
//<editor-fold defaultstate="collapsed" desc="Class Declaration">
/**
*
* @author COMP1102
*/
public class Lab1_4 extends JApplet {
public static void main(String [ ] args){
}
/**
* Initialization method that will be called after the applet is loaded into
* the browser.
*/
public void init() {
// TODO start asynchronous download of heavy resources
setSize(400,300);
}
// TODO overwrite start(), stop() and destroy() methods
//</editor-fold>
public void paint(Graphics page) {
//Add your code here
page.setColor (Color.red); //Red, top left corner
page.fillRect (0, 0, 200, 150);
page.setColor (Color.blue); //Blue, top right corner
page.fillRect (200, 0, 200, 150);
page.setColor (Color.yellow); //Yellow, bottom left corner
page.fillRect (0, 150, 200, 150);
page.setColor (Color.green); //Green, bottom right corner
page.fillRect (200, 150, 200, 150);
page.drawLine (0, 150, 200, 0); //Diagonal lines
page.drawLine (200, 0, 400, 150);
page.drawLine (0, 150, 200, 300);
page.drawLine (200, 300, 400, 150);
page.setColor (Color.yellow); //Yellow part of centre circle
page.fillArc (200, 150, 100, 100, 0, 90);
page.drawArc (200, 150, 100, 100, 90, 180); //Uncoloured part of centre circle (red sector)
page.setColor (Color.blue); //Blue part of centre circle
page.fillArc (200, 150, 100, 100, 180, 270);
page.drawArc (200, 150, 100, 100, 270, 360); //Uncoloured part of circle (green sector)
}
}[/code]
Fixed it but your thing doesn't show up like it's supposed to but it's close.
[code]//********************************************************************
// Snowman.java Author: Lewis/Loftus
//
// Demonstrates basic drawing methods and the use of color.
//********************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Snowman extends JApplet {
//-----------------------------------------------------------------
// Draws a snowman.
//-----------------------------------------------------------------
public void paint (Graphics page)
{ final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50);
// ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80);
// sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40);
// head
page.fillOval (MID-35, TOP+35, 70, 50);
// upper torso
page.fillOval (MID-50, TOP+80, 100, 60);
// lower torso
page.setColor (Color.black);
page.fillOval (MID-10, TOP+10, 5, 5);
// left eye
page.fillOval (MID+5, TOP+10, 5, 5);
// right eye
page.drawArc (MID-10, TOP+20, 20, 10, 190, 160);
// smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40);
// left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60);
// right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5);
// brim of hat
page.fillRect (MID-15, TOP-20, 30, 25);
// top of hat
} } [/code]
What about this? It's the demo code that my lecturer used, lacks a main method but it works?
[QUOTE=download;35102510][code]//********************************************************************
// Snowman.java Author: Lewis/Loftus
//
// Demonstrates basic drawing methods and the use of color.
//********************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Snowman extends JApplet {
//-----------------------------------------------------------------
// Draws a snowman.
//-----------------------------------------------------------------
public void paint (Graphics page)
{ final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor (Color.blue);
page.fillRect (0, 175, 300, 50);
// ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80);
// sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40);
// head
page.fillOval (MID-35, TOP+35, 70, 50);
// upper torso
page.fillOval (MID-50, TOP+80, 100, 60);
// lower torso
page.setColor (Color.black);
page.fillOval (MID-10, TOP+10, 5, 5);
// left eye
page.fillOval (MID+5, TOP+10, 5, 5);
// right eye
page.drawArc (MID-10, TOP+20, 20, 10, 190, 160);
// smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40);
// left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60);
// right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5);
// brim of hat
page.fillRect (MID-15, TOP-20, 30, 25);
// top of hat
} } [/code]
What about this? It's the demo code that my lecturer used, lacks a main method but it works?[/QUOTE]
Hmm that is weird.
Your code and that code both work in Eclipse fine.
[QUOTE=Doritos_Man;35102590]Hmm that is weird.
Your code and that code both work in Eclipse fine.[/QUOTE]
Maybe because it's an applet? Just a guess.
Neither of those codes (my original, mine modified, and the demo code) work in netbeans. I'm thinking something bigger is off. I'm still getting the "No main classes found" error
When you create your new class file, are you creating just a regular default class? You need to make sure you select Java Main Class from the file menu. That should fix it.
Fixed it.
Since you're using an Applet you don't need a main method.
Just right click on the Applet form and click run.
You can also launch it with an html file:
[code]
<html>
<body>
<applet code="lab1_4.Lab1_4.class"
archive="relative OR full path to compiled .jar"
width="400" height="300" >
</applet>
</body>
</html>
[/code]
Sorry, you need to Log In to post a reply to this thread.