I am trying to practice this example:
Creating your first Java program
This is where the fun begins ! Let's create a simple greetings class. Use your favorite text editor to create a new file. You don't need any advanced
IDE like Eclipse or IntelliJ yet. For the moment, stick to the editor you're used to. Copy/paste the following code in your editor :
public class MooseGreetings {
public static void main(
String[] args) {
System.out.println("mooooooooooo");
}
}
Save the file in the directory of your choice. But do not save it under the JDK install directory. Instead, save it in a directory where you'd like to keep your java sources in. For example, C:\java\src. Name the file MooseGreetings.java. Be extra careful with the uppercase and lowercase. The filename must match exactly the name of your class. mOoSegReeTings.java won't work. Also be careful that your editor does not append the ".txt" extension to your file. Some filthy editors like Windows Notepad may do it, but Notepad is probably not your favorite editor !
Compiling and executing the program
(For Mac-specific instructions, go to
https://coderanch.com/t/111632/Mac/JAVA-MAC-OS)
At the command prompt, go to the directory where you have saved the MooseGreetings.java file. Type "javac MooseGreetings.java", and press the enter key.
C:\>cd \java\src
C:\java\src>javac MooseGreetings.java
The class should be compiled, and a new file called MooseGreetings.class should be created in the same directory. If a compiler error happens, make sure you copy/pasted the above code properly. If the file is not found, make sure you're in the right directory, and that your file is properly named MooseGreetings.java. If you can't figure out why it does not compile, ask at the Beginner Java forum.
Let's execute the program. In the same directory, type the following command : "java -classpath . MooseGreetings".
C:\java\src>java -classpath . MooseGreetings
mooooooooooo
C:\java\src>
Greetings for the Javaranch Moose should be printed : mooooooooooo.
That's it ! You've successfully executed your first Java program. You're now ready to get into the Java world ! If you have any questions, some friendly folks will be happy to help you at the Beginning Java forum.