Tuesday, January 24, 2012

How to run NetBeans compiled program

Last article show how to create a Hello World program using NetBeans IDE 7.1, and also run it - inside NetBeans IDE. If you try to run it in command line using java (ex. java HelloNetBeans), may be you will be complaiined with Error of java.lang.NoClassDefFoundError. It should be a normal case.

Now, click Run -> Clean and Build Main Project in NetBeans IDE top menu. NetBeans' Output window will show you how to run it in command line. such as:
java -jar "C:\Users\erix7\Documents\NetBeansProjects\helloNetBeans\dist\helloNetBeans.jar"

NetBeans IDE Output show you How to run NetBeans compiled program
Run the generated jar using java in command line

Hello World! using NetBeans IDE 7.1

To verify our installation of NetBeans 7.1, we are going to create a helloworld using NetBeans.

- Click File -> New Project... in NetBeans IDE top menu.

- Select Java from Categories, and Java Application from Projects. Then click Next.
New Java Application

- Enter helloNetBeans in Project Name field, make sure both checkbox of Create Main Class and Set as Main Project is checked. the Main class name will updated automatically when we enter Project Name. Click Finish.
Enter Name and Location

- A default project will be generated for you.
Auto generated code

- Modify the code in main() to say something.
public class HelloNetBeans {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World! from NetBeans.");
}
}


- Final Save(File -> Save), Build (Run -> Build Project) and Run (Run -> Run Main Project). If everything go correctly, you can see the word "Hello World! from NetBeans." in the Output windows, that means your installation is properly.
Hello World shown in Output window

Related Article:
- How to run NetBeans compiled program

Sunday, January 22, 2012

Hello World of Java 7 on Windows 8, in "cmd" DOS Prompt

After installed of Java environment, it's time to code our first Java program - helloworld.

The first exercise of helloworld we create and compile in "cmd" (DOS Prompt) environment.

- Create a text file helloworld.java, using any editor program you like.

- Entent the code, and save it:
public class helloworld{

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

}

helloworld.java

- Start a "cmd" window in Windows 8, refer to the post "Run 'cmd' in Windows 8" if you don't know how. Change to the directory of helloworld.java.

- Type the command to compile the java program:
javac helloworld.java

- Type the command to run the compiled class:
java helloworld

- If your JDK environment is set properly, the output should be:
Hello World!

helloworld.java

*** If you cannot run javac/java due to missing in path setting, refer to the post "Set PATH for JDK 7 on Windows 8".