Skip to main content

macosx problems running .class files

1 reply [Last post]
macssuck
Offline
Joined: 2006-02-17
Points: 0

when i compile a project through drjava, it makes a .class file that i am supposed to execute. so i try it and it says to check the console. When i do it shows this:

java.lang.NoSuchMethodError:main

i have no idea why it is doing this. I am in as admin, but i dont know how much longer i will have access because i am at school doing this....

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pheet
Offline
Joined: 2004-11-27
Points: 0

Hiya,

Java produces a class file for every object defined. For a class to be executable, it must have a main method:

[code]
public static void main(String[] args)
[/code]

Normally when writing a program you define several classes, each public class in its own .java file, and you have one class with a main method that you execute.

eg.

[code]
public class Hello{

public static void main(String[] args){
System.out.println("Hello world");
}
}
[/code]
is saved as a file called Hello.java, is compiled by java compiler javac to Hello.class and can be executed by the JVM java (from a command line you can run Hello with $ java Hello ).

This:

[code]
class MyObject{

private int value=0;

public MyObject(int startvalue){
this.value=startvalue;
}

public int getValue(){
return value;
}
}
[/code]

defines a class for MyObject which would be saved in a file called MyObject.java and compiled to MyObject.class but is [b]not[/b] executable.

If your class has a mian method, make sure you've named it correctly - java is case sensitive so MyClass.java and Myclass.java are different. Is your IDE set to look in right place, ie where the class files are, when running the JVM?

You can read about some basic Java stuff, try http://java.sun.com/docs/books/tutorial/ .

HTH,

Pheet