How to run a Java Class from scratch

Java programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to run a Java Class from scratch

Post by Neo » Sun Apr 18, 2010 12:55 am

I had to write a Java program to help a member of ours. I didn't touch Java after 2003 and I couldn't remember the essential settings. So I had to surf the net to find out how we can compile and run a Java class file. So I thought it would be better to add a quick guide to do this from scratch.
  1. Download Java Development Kit (JDK).
  2. Set paths - You better copy below code to a batch file (.bat) to run before compiling and running a Java program.

    Code: Select all

    set path=C:\Java\jdk1.6.0_20\bin
    set CLASSPATH=.;C:\Java\jdk1.6.0_20\lib
    set JAVA_HOME=C:\Java\jdk1.6.0_20
    
  3. Compile a Java program - use following command

    Code: Select all

    C:\Java\jdk1.6.0_20\bin\javac.exe Hello.java
    Sample Java code is give below. Save following contents to Hello.java

    Code: Select all

    public class Hello {
         public static void main(String argv[]) {
              System.out.println("Hello world !");
         }
    }
    
    After this step, you can see a file named as Hello.class
  4. Running the class file - use following command

    Code: Select all

    C:\Java\jdk1.6.0_20\bin\java.exe Hello.class
That's it! I hope you will be able to run your first Java program.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: How to run a Java Class from scratch

Post by Nipuna » Mon Feb 21, 2011 12:19 pm

Neo friend

I did as this

http://www.java.com/en/download/help/path.xml

http://www.ehow.com/how_5127784_set-java-path.html


Then I don't have to Type Java Compiler Path again and again.

We can use Simple by typing

Code: Select all

javac 
in Command Prompt

If some one wants more info Just search on Google like this " set java path "

I added this to Help for others and Not tried to Show that I Know More than You. Don't misunderstand Me !
Post Reply

Return to “Java Programming”