LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting started with Java, compiling HelloWorld.java (https://www.linuxquestions.org/questions/programming-9/getting-started-with-java-compiling-helloworld-java-708237/)

baldurpet 02-28-2009 01:23 PM

Getting started with Java, compiling HelloWorld.java
 
Hey, I know this is a worn out topic but all the search results I found on Google told me to apt-get a program which wasn't in the repository.

I basically need to compile a simple java program, and run it.

The program is just a simple Hello World program:

Code:

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


Hko 02-28-2009 01:58 PM

Save that code to a file with the "HelloWorld.java" (same name as public class + ".java").

In a shell go to the direcotory where "HelloWorld.java" is stored, and type the command: javac HelloWorld.java

Then run it with the command: java HelloWorld

baldurpet 02-28-2009 03:59 PM

Quote:

Originally Posted by Hko (Post 3460653)
Save that code to a file with the "HelloWorld.java" (same name as public class + ".java").

In a shell go to the direcotory where "HelloWorld.java" is stored, and type the command: javac HelloWorld.java

Then run it with the command: java HelloWorld

Thank you!

This is so much hassle though, it creates a HelloWorld.class file and you have name the file the same as the public class (you don't have to do that in C++ or Python).. Is it possible to compile and run the program in the same command? Most of the time you just want to test the program and you don't need the .class file.

Why do you write $ java HelloWorld though in the second step? There isn't any file or folder called HelloWorld, what file does the program call and why don't you write $ java HelloWorld.java or $ java HelloWorld.class? What does the .class file do anyway?

AceofSpades19 02-28-2009 04:37 PM

Quote:

Originally Posted by baldurpet (Post 3460757)
Oh wow thank you!

This is so much hassle though, it creates a HelloWorld.class file and you have name the file the same as the public class (you don't have to do that in C++ or Python).. Is it possible to compile and run the program in the same command? Most of the time you just want to test the program and you don't need the .class file.

Why do you write $ java HelloWorld though in the second step? There isn't any file or folder called HelloWorld, what file does the program call and why don't you write $ java HelloWorld.java or $ java HelloWorld.class?

The java command automatically adds .class to the filename, the .class file is bytecode which is like machine code for the Java Virtual Machine(JVM), the .java file is just source code which the JVM doesn't understand.

paulsm4 02-28-2009 04:59 PM

Hi -

Quote:

This is so much hassle though, it creates a HelloWorld.class file and you have name the file the same as the public class (you don't have to do that in C++ or Python).. Is it possible to compile and run the program in the same command? Most of the time you just want to test the program and you don't need the .class file.
Yes, of course it's possible. Here's an example of one such command, "doit". It takes three steps - but you only have to do them once:
Quote:

1. vi doit
Code:

if [ $# != 1 ]; then
  echo ERROR: You must specify program
else
  javac $1.java
  java $1
fi

2. chmod +rx doit

3. ./doit
PS:
Google for "Netbeans" or "Eclipse" (two excellent Java IDE's); and "ant" or "maven" (for automated Java build and deployment tasks). All Open Source and freely downloadable.

jlinkels 02-28-2009 05:51 PM

When I tried my first steps on the slippery road to Java, I found this website helpful: http://java.sun.com/docs/books/tutorial/

Ant was a great help also for me when I had to compile a large project without any instructions: http://ant.apache.org/

jlinkels


All times are GMT -5. The time now is 12:47 AM.