ZaSz
2010-01-20 20:51:41
Zasz's Making your first Java Program Tut.
Getting an IDE
Getting an IDE
Well, if you want to make a java program, I would 100% recommend an IDE, because it will correct your syntax and everything.
The IDE I will be working with is Eclipse, and I will use it throughout the tutorial, as it is an excellent IDE in my opinion.
You can get Eclipse here. I personally would recommend the Eclipse IDE for Java EE Developers (189 MB), but if your only aiming for standard development, go with the Eclipse IDE for Java Developers (92 MB).
Creating the source file.
Right click in the projects window, and click new java project. (see picture)

Then use the drop down arrow on the new project to expand the project. Then right click on the src folder and click make a new class file. (Pic)
A popup will come up, and all you need to do is fill out the Name field and click finish, you don't have to mess with package or any of that other stuff.
Making a Hello World.
Lets say I name the class file test. In the main section, test.java will popup with
public class test {
}
in the debug box.The public class test is like Program asdasd; in scar. Except that Class (name) must match the file name. So if it was
public class me it would have to be saved as me.javaNow we will create the main procedure that will be called when the program is ran. All we have to do is add.
public class test {
public static void main(String[] args) {
}
}
Main will be called as soon as we run the program. We could easily just make it write out hello world right from that procedure, but I want to show you how to do a few different kinds of procedures.
public class test {
public static int hi(){ // int means we have to return an integer.
System.out.println("Finding 50+50");
return 50+50; // Will return 100. You can just put 100, but Im saying you can use equations for the return.
}
public static boolean hi(String see){ // boolean means we would have to return an boolean. We can also have
//Duplicate names of procedures if the parameters are different (So they can do different names)
if(see=="yessss"){
System.out.println("Finding if 50+50 = 100!");
return (50+50)==100; // Could've said true
} else {
return (20+60)==100; // Could've said false. But an equation example
}
}
public static void main(String[] args) {
System.out.println("Hello, Lets see an int."); // This is like a println from scar. But we are calling from System.out
//Println is a lot like scar. You don't have to use "" for non strings.
/* Using the /* will block
out anything after until you close it. Much like // except if you
want to block out large lines of code.
*/
System.out.println(1); // Will write out the #1.
System.out.println(hi()); // Will write out 100
System.out.println(hi("yessss")); // Will write out true;
System.out.println(hi("blah")); // Will write out false;
WriteDebug("We can make a shorter system.out.println!");
WriteDebug("Hello World!");
}
public static void WriteDebug(String message) { //Procedure names are case sensitive also!
//Also, it doesn't matter where the procedure is in the program compared to the main procedure.
System.out.println(message);
}
}
Save your first program. (ctr-s = fastest)
Running your first program!
The most easiest and quickest way to do this is by batch.
First find your project directory, then find your source file.
Right click in the folder. And make a new text document.
Paste this code into the document.
@ECHO OFF
ECHO Looking for JDK
cls
SET KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Development Kit
FOR /F "tokens=3" %%A IN ('REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>NUL') DO SET jdkv=%%A
SET jdk=
IF DEFINED jdkv (
FOR /F "skip=2 tokens=3,4" %%A IN ('REG QUERY "%KEY_NAME%\%jdkv%" /v JavaHome 2^>NUL') DO SET jdk=%%A %%B
) ELSE (
FOR /F "tokens=*" %%G IN ('DIR /B "%ProgramFiles%\Java\jdk*"') DO SET jdk=%%G
)
SET jdk=%jdk%\bin
SET javac="%jdk%\javac.exe"
IF NOT EXIST %javac% (
javac -version 2>NUL
IF "%ERRORLEVEL%" NEQ "0" GOTO :notfound
) ELSE (
GOTO :setpath
)
GOTO :eof
:notfound
ECHO JDK is not installed, please download and install it from:
ECHO http://java.sun.com/javase/downloads
ECHO.
PAUSE
:setpath
SET PATH=%jdk%;%PATH%
javac test.java
pause
cls
java test
pause
GOTO :eof
The only thing you need to edit is at javac at the end, edit that to your programs java name. Then edit java test to java "your programs name without the .java"
Go to file-save as- Then name it something like "Compile Test.bat" - Then change Save as type : Text Documents (.txt) to all files.
Click save. Now your program is ready to run! Just double click the .bat to see it in action!

Maybe my next will include gui?