tags:

views:

211

answers:

7

I have an error in my first step with Java, so when i try to run the code hello world:

class apples{
    public static void main(String args[]){
        System.out.println("Hello World!");
    }
}   

I go to: - Run as .. -> Then i choose Java aplicacion - > And i press Ok (http://yfrog.com/joerror2np)

But when i press Ok does not appear the window down to show me the correct message Hello World, so i get the error of screenshot http://yfrog.com/5merror3zp

A: 

Try public class apples and make sure the file is apples.java. Also it should be public static void main(String[] args)

Raptrex
It doesn't have to be public. The name only has to match if it is public. And `String args[]` is valid syntax, although it's discouraged.
Matthew Flaschen
A: 
  • You class must be named "thesame" if you store it in a file called "thesame.java", as you have. Either rename your class to "thesame" or change the file to be "apples.java".

  • You should move the "[]" to be before "args". So, String[] args.

  • Either select "apples" at the bottom of the menu you posted and run it, or right-click on the Java file and make it the default thing to run for this project. Or launch it by right-clicking on the file and selecting "run".

Borealid
`String args[]` works just fine.
NullUserException
@NullUserException: I generally feel that new programmers should associate the "array-ness" with the type, and not the variable name; so, it's `args` which has type `String[]`. Thus the rearrangement.
Borealid
"You class must be named "thesame" if you store it in a file called "thesame.java"". Only if it's public, which it's not. Regarding the array, it's fine to state that most people prefer `String[]`, but you should note that's not the issue.
Matthew Flaschen
+8  A: 

Your code works fine for me:

class apples
{
    public static void main(String args[])
    {
        System.out.println("Hello World!");
    }
}

I downloaded it to c:\temp\apples.java.

Here's how I compiled and ran it:

C:\temp>javac -cp . apples.java

C:\temp>dir apples
 Volume in drive C is HP_PAVILION
 Volume Serial Number is 0200-EE0C

 Directory of C:\temp

C:\temp>dir ap*
 Volume in drive C is HP_PAVILION
 Volume Serial Number is 0200-EE0C

 Directory of C:\temp

08/15/2010  09:15 PM               418 apples.class
08/15/2010  09:15 PM               123 apples.java
               2 File(s)            541 bytes
               0 Dir(s)  107,868,696,576 bytes free

C:\temp>java -cp . apples
Hello World!

C:\temp>

Your lack of understanding and the IDE appear to be impeding your progress. Do simple things without the IDE for a while until you get the hang of it. A command shell and a text editor will be sufficient.

Sorry about missing javac; cut & paste error.

duffymo
You don't have a single `javac` command in the excerpt you posted. Also, he's using Eclipse, so let's not scare him off with the command line, eh?
Borealid
@Borealid, he missed that in his excerpt, but I also tested it, (with `javac`), and it does work.
Matthew Flaschen
@Borealid: It's however a good way to get the grasp how IDE's works "under the hoods". Classpath and so on. At least, duffy has nailed the problem down: there's no problem, no error, it's just ignorance how the IDE work. I would first learn Java and then Eclipse.
BalusC
Is working - Guys are you faster more like a Ferrari .., many thanks.. This page go right in to my favorites :)
@vili: you should then mark this answer accepted :) See also http://stackoverflow.com/faq
BalusC
To be clear, it will also work if the filename is somefile.java.
Matthew Flaschen
A: 

Class names must be capitalized... so change apples to Apples. Also, if you are a beginner (which it seems like), I would recommend the Netbeans IDE -- it's a bit more friendlier for new users than Eclipse.

CD Sanchez
Actually, capitalization is a convention, not a requirement.
Tassos Bassoukos
+1 for recommending Netbeans, -1 for the "class names must be capitalized" part. No they don't.
NullUserException
+3  A: 

If you look at the screenshot, your class name is there, last in the list. Select it and press OK. To not see this message again, right-click on the class name on the left side and select there Run...->Java Application.

Tassos Bassoukos
A: 

You have 2 classes by name of "thesame.java" under the source folder. Since one is directly under the src folder, and other under (default package), they use the same namespace, hence Interpreter is confused which java file to execute and is asking you to select the class you want to execute.

Logan
+2  A: 

The only problem that causes your error here is that the classname and the filename do not match - and they have to.

Solution

Rename either the file thesame.java to apple.java or the class to thesame. Then if you select "Run as..." again, eclipse will present a menu item to start your Java application.

(other mentioned, that there's no requirement that a top-level class and the filename do match - unless the top level class is public. Of course this is true. But the problem was about "running" a class under eclipse as a Java application)

Andreas_D
Several people have already suggested this, but it's wrong. The class name and filename only have to match if the class is public.
Matthew Flaschen
@Matthew, we can't 'Run' a class with eclipse when class and classname do not match. Tested it with eclipse 3.6 and the question was about eclipse. In general, you're right, but the answer is true in the questioners context.
Andreas_D
Tassos explained how to run it in Eclipse, so I don't think it's impossible to do so.
Matthew Flaschen