![]() |
|
Your First Java Program - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: Your First Java Program (/Thread-Your-First-Java-Program) |
Your First Java Program - Solixious - 03-22-2013 Your First Java Program So today I'm going to teach you how to write, compile and execute your first java program. It is a simple program to display "Hello Hack Community" as the output. What do you need? You need nothing but JDK installed in your machine. We are going to write our code in Notepad and compile it via command prompt. Step 1 Write the following code in a new blank Notepad document. Code: class HelloHC
{
public static void main(String args[])
{
System.out.println("Hello Hack Community");
}
}![]() Step 2 Save the code in your projects folder. Make sure you save the file with same name as the class name (HelloHC in this case) and with an extension of .java Select the type All Files instead of Text Documents(*.txt). ![]() Step 3 Now it is time to compile your code. Navigate to your projects folder using command prompt. Type in the following command Code: javac HelloHC.javaIf you did not get any error message, then your file has been compiled successfully. However, if you get an error message : Code: 'javac' is not recognized as an internal or external command,
operable program or batch file.You need to add the path of JDK's bin directory. Code: path [Drive:]\[Path of jdk\bin Directory]This would be followed by the former command you gave i.e. javac HelloHC.java After compiling your code successfully, a new file would be created in your projects folder with same name HelloHC.class Now in order to execute the code you just compiled, just type in the following command Code: java HelloHC![]() Congratulations! You just executed your first Java Program! Cheers ![]() Updating the PATH Environment Variable You can run the JDK without setting the PATH environment variable, or you can optionally set it so that you can conveniently run the JDK executable files (javac.exe, java.exe, javadoc.exe, and so forth) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable file every time you run it, such as: Code: C:\> "C:\Program Files\Java\jdk1.7.0\bin\javac" MyClass.javaIt is useful to set the PATH variable permanently so it will persist after rebooting. To set the PATH variable permanently, add the full path of the jdk1.7.0\bin directory to the PATH variable. Typically, this full path looks something like C:\Program Files\Java\jdk1.7.0\bin. Set the PATH variable as follows on Microsoft Windows:
Note:
Source : Oracle's Documents RE: Your First Java Program - Deque - 03-23-2013 Thanks for this. -stickied- I have two suggestions for improvement: For usage of JDK as you describe it you have to update the PATH variable on Windows. That is not trivial for most people. So maybe you can add a description how to install JDK properly, how to update the environmental variable, where to get the JDK from and also one sentence what it actually is (Java Developement Toolkit). Here is a description about updating the PATH: http://docs.oracle.com/javase/7/docs/webnotes/install/windows/jdk-installation-windows.html#path The HelloHC.java has no indentation at all. Can you add a proper one? RE: Your First Java Program - Deque - 03-23-2013 Thanks for this. -stickied- I have two suggestions for improvement: For usage of JDK as you describe it you have to update the PATH variable on Windows. That is not trivial for most people. So maybe you can add a description how to install JDK properly, how to update the environmental variable, where to get the JDK from and also one sentence what it actually is (Java Developement Toolkit). Here is a description about updating the PATH: http://docs.oracle.com/javase/7/docs/webnotes/install/windows/jdk-installation-windows.html#path The HelloHC.java has no indentation at all. Can you add a proper one? RE: Your First Java Program - virusreloaded - 05-03-2013 Nice Tutorial mate... but you should show other way to create path using My Computer > Properties >Advance setting. That's much easier to understand as many beginner find GUI comfortable as compared to CLI .. This was just a suggestion ...
RE: Your First Java Program - noize - 07-03-2013 I noticed here is not told any way to add directories to the PATH variable. This could be done from explorer.exe by following Computer -> System properties -> Advanced system settings -> Advanced -> Environmental variables, or in an easier way from cmd.exe: Code: set path=%path%;C:\newPathIWantToAdd\The above command would just temporarily append a new path to the PATH variable, to save changes you should then use: Code: reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %path% /fRE: Your First Java Program - loserman - 07-09-2013 learning right now form college and making a good project for student feedback
RE: Your First Java Program - Buster - 07-31-2013 I want to learn about hacking, already know basics of both Java and C. Which language would you guys prefer me to learn? RE: Your First Java Program - Solixious - 07-31-2013 Maybe this will help you.. http://www.hackcommunity.com/Thread-Programming-language-FAQ-and-choosing-guide RE: Your First Java Program - Buster - 07-31-2013 (07-31-2013, 02:17 PM)Solixious Wrote: Maybe this will help you..Ah thankyou very much, I am searching for some thing like this :epic: RE: Your First Java Program - blackeagle - 09-24-2013 u should use netbeans for java programming if i'm not wrong it auto-compile the program u just right it and hit run we use it at university !! |