Compile!
We must first compile the program before we can run it. Save the program with same name as the class. You MUST save the same name as the class, in this case, Hello.java. So it will work properly. There are 2 ways, using command prompt or JDK directly, or using user-friendly interface. I am using BlueJ presently, so I’ll show that one. There is a right term for this program… but I can’t remember now.
1. Compile using command prompt
Open the command prompt. First, set the path to be able to use compiler. There is a way so we don’t need to do this every time, but I can’t remember how. Set the path to where you save it/install your JDK, usually just as where you can see below.
Test if you have set the path by entering this. If succeeded, there will be many lines of… you will see.
But before you can compile, you should change to the directory you have saved the program. To change directory, enter cd “name of directory”. To change back up, use cd.. To change to top tree of file directory, use cd/. To change drive, use D: to change to drive D. Because we can be lazy, I used batch file and just click it. Hehe. This is my java batch file, java_1.7.bat .
Read more about command prompt commands should the need arise.
Run the following command to compile.
If set up properly, javac will run for a short while and produces file named Hello.class.
To run the program:
This command should produce output of Hello World! In your command prompt. Java is java interpreter program, it runs the Virtual Machine. This is in regard of Java program saved in form of binary. It need Java Virtual Machine to run. Therefore, it can be run in any system having this JVM. While in any other programming language, it required to be compiled and run according to the system.
2. Compile using BlueJ
BlueJ is something like Borland C++. Make life easy, but have way too much file in it. I usually use DevC++ to compile C++. But for Java, it started to feel a lot easier to compile using this… BlueJ. I still didn’t remember the right term. Oh well.
First, download and install BlueJ. Then, you should have need to set path first when you first start the program. Press at the menu Project >> New Project.
Set your directory where you want to save it. Then put in the folder name box your project name. For this case, I put Hello World.
Then press Create. It will create a folder named Hello World with the icon penguin on it, if you view using BlueJ. I’m serious.
Oh, then press new class, enter the name of class and press OK (use Hello). You can see the class Hello appears as a box there.
If you double-click, you should see various things in it, a standard way of writing a class. But you don’t need them all right now. You may read them, but I’ll just clear em out… and enter my program in. Stand-alone program for now doesn’t use whatever you read there much yet. That documentation is good practice though.
Click Compile button to compile. Close or just switch to main window of BlueJ. Right-click Hello class and press the void main (String [] args). And just press enter again.
A terminal window will show up, showing how it would appear in command prompt screen.
Because this is easier in my opinion, I use IDE instead of command prompt now. Oh, I finally remember the term. Integrated Development Environment, IDE.
No comments:
Post a Comment