Failed at the first tutorial already

+1 Gabrie van Zanten · December 2, 2015
Hi
I wrote the first code like this:

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

Saved it as "youtube.java" in "/Users/gabrie/stack/JavaProjects". 
Then I compile it with:
javac youtube.java

And as a result got the "apples.class". But when I now run this I get the following error:
Error: Could not find or load main class apples.class

Doing some Google learned that it probably has something to do with the path settings, but I can't get it to run. Tried these options:

java apples.class
java -cp . apples.class
java -classpath . apples.class
java -cp /Users/gabrie/stack/JavaProjects apples.class
java -classpath /Users/gabrie/stack/JavaProjects apples.class
java -classpath /Users/gabrie/stack/JavaProjects:. apples.class

I'm always running this from /Users/gabrie/stack/JavaProjects

My Java version is:
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

Running this on OSX 10.11.1

Any help would be appreciated.
Gabrie

Post a Reply

Replies

Oldest  Newest  Rating
+1 Vitaliy Zet · December 2, 2015
When running java (java class) you shoud specify only name of the class. Without extension. 
E.g. "java apples", not "java apples.class" 
Note: by code convention, you should use a capital first letter in the class name. Like: Apples
0 Gabrie van Zanten · December 2, 2015
Thank you, that was the trick :-)

And noted the remark on the capital first letter.
0 Nicholas Eason · December 3, 2015
Also, your class is called apples but you tried to compile one named Youtube. The class and filename need to match up :P
0 Gabrie van Zanten · December 4, 2015
Strangly enough that is how it is done in the videos
0 Prashant Sethi · December 5, 2015
I don't think it is mandatory all the time to have the class and filename match up. Of course it makes it easier to find classes when you have ton of files in a project, and also avoids the confusion to remember the different filenames and classnames.

There are some cases though I read about when these two should match up. I am a beginner too and I cannot recall that.
  • 1

Java / Android Development

107,195 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator