Java Basics:Getting Started with Java

Here we will discuss some basics topics related to Java.

  1. Where to download Java.
  2. How to install Java.
  3. Setting up the Environment Variables.
  4. Our First Java Program.
  5. How to compile a Java application.
  6. How to run a Java Application.

In next topic we will discuss about the difference between important terms in Java (JDK vs JRE vs JVM).

How to Download Java

The latest version of Java can be downloaded from Java Website. Current version is 17 which is LTS release. It is always good practice to use LTS release than any release in PRODUCTION. Now the question is what is LTS. LTS stands for Long term support. It means Oracle will provide support for given version for longer period that other versions. Java SE 7, 8, 11 and 17 are LTS releases. If you want to more about the LTS releases you can head to this support roadmap link.

Java Installation

There are no special requirements when installing Java.  Be sure that you have suitable permissions on your computer to install software. It can be installed like any other software. Be sure to download correct Java package for your system based on OS.

Setting up the Environment Variables

After installing Java there are some environment variables that need to be set.

  • CLASSPATH: This environment variable points to the location of the JDK home directory. It also contains the address of folder from where jars get loaded by ClassLoader (For more details of ClassLoader visit here)
  • JAVA_HOME: This environment variable will point to the location of the Java home directory.

How to Check if Java is Installed

To check if your Java is installed properly open Command Prompt. To open command prompt write “CMD” in run command and hit enter. In the command prompt window write “java -version“.  If your Java is installed properly and all environment variables are configured correctly it will show the version of Java installed. Information reflected on the command prompt will be like

C:\Users\Jbt>java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) Client VM (build 25.51-b03, mixed mode, sharing)

If there is any problem while installing or in setting up the environment variable, the output on the command prompt will be as shown below.

'java' is not recognized as an internal or external command,
operable program or batch file.

How to check if Java is up to date

To know if the Java installed on your system is up to date Click Here.

Our First Java Program

Though the popular first program to write in Java is” Hello World !! “, here we will write a program to print ” Hello JBT !! “. 🙂

Open an editor and write the below code.

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

Save the file with the name “FirstProgramme.java” in folder “c:\jbt”. Please note that name of the file should be same as the name given to public class(For more details regarding class file rules click here). Once the file is saved, open the command prompt and change the working directory to “c:\jbt”, where your file is saved. Write “javac FirstProgramme.java to compile the Java code as below.

C:\Users\JBT>cd C:JBT

C:JBT>javac FirstProgramme.java

C:JBT>

If the java file is compiled properly the compiler will create a class file for the source java. It will be saved in the same location as the source file. Since no package is declared in the given code the .class file will be created in the same folder location.

Notice the difference for Java files using package declaration. Let’s create another Java file as below.

package com.jbt;

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

Save this file as “FirstProgrammeWithPackage.java” to “c:\jbt”. Now go to the command prompt and execute the below command.

javac -d . FirstProgrammeWithPackage.java

It will create the class file in a corresponding package(com.jbt).

Now that you know how to compile Java file with a package and without a package, the next step would be run these class files.

How to Run Java Application

Now that your  Java file is compiled we can execute the application using the “java” command as below.

Without Package

C:\JBT>java FirstProgramme
Hello JBT!

C:\JBT>

With Package

C:\JBT>java com.jbt.FirstProgrammeWithPackage
Hello JBT!

Or

C:\JBT>java com/jbt/FirstProgrammeWithPackage
Hello JBT!

Note*: The “java” command uses the class file name without its extension(.class).

With this, we are done creating and running our very first Java application.

In the next chapter, we will learn the difference between JDK and JRE.

175 Comments

  1. Am a student of medicine who is trying to learn JAVA and belief you me , your tutorial is exceptional . I really love the systematic explanation.
    On point. We need people like YOU to help us understand software environment.

  2. I accidentally landed on your page while I am googling for Java Tutorial for my nephew.
    I have gone over this blog. The Tutorial was been built with very good thought process.. I would say If one spends their time, they certainly end up with strong foundation level in JAVA which I feel many of the IT professionals certainly need.

  3. I am graduated in finance and know nothing about computer programming. But I am so interested in becoming a software engineer. Can you please tell me if learning java will help me for the same?

    • Hi Prabhavathy,

      I really don’t know the answer of this question. Just knowing java will not solve all your problem. But it will help you definitely. And learning java is not difficult unless you have good teacher.

      Regards

    • I would like to share my real time experience here. I would say if you love programming go for it. Learning Java, gives you strong foundation for any IT professional line you desire. There are so many opportunities in IT like Business Analyst, Quality Assurance where one would expect to bring programming skills and Java is one among them. Within QA there are Automation programming tools like selenium. You can take this knowledge (Programming skills) and apply these skills in learning any other tool or building a Programming career (Entry level). Hope this helps!

  4. can i start learning java core even if i dont know C language or any basic concepts of c
    like even loops or syntex or even 0.% knowledge of c
    CAN YOU RECOMMEND ME TO START JAVA CORE ?????

  5. This tutorial is very helpful as I have just started learning ,I am getting things in correct manner ,and by following steps that are mentioned in the article, I’m getting things very easily and definitions are also very clearly understood.. 🙂

    Thanks & Regards,
    Komal

    • package is way of organizing java classes inside a project. You needs to have unique class name inside a package name. But class with same name can exist in different packages.

  6. Hey ,
    Thank You for providing such awesome courses of core Java to students. You are doing a great work indeed.
    I got a suggestion for you.
    I have seen that there are a few untapped topics in this “Getting started with Java” tutorial , which I found are in the following article with the same topic.
    I would suggest you to add the matter here which will be beneficial for the students/readers.

    Thanks again you this awesome work. Hope you will take my words into consideration.

  7. Hello Sir/Madam,
    Actually i am planning to move another project, for that management team asked me to learn Core JAVA, so is it possible to learn at least basic concepts of CORE JAVA within ten days?????
    Please suggest me ASAP,
    Would you please reply me at your earliest convenience so that i can address your suggestions?

    Thanks in advance!!!
    Giri.

    • Hi Giri,

      There is no direct answer to this question. It all depends on where you are coming from. If you have learned C++ and then i might say you can learn a bit about Java. But in the end 10 is not enough to learn any language. But still in indian companies you don’t need to be rocket scientist to work on any project. So just start learning java soon you will learn enough to work.
      Best of luck for your new project.

      Regards

    • There is no direct linking between C++ and Java. So yes you can learn java without knowing C++. Knowing C++ will help you in java, as some concepts are common but that will give you starting advantage but still you can learn java from scratch.

  8. hey..I have installed java using sudo apt-get install openjdk-7jdk. i am able to compile programs but not run any. if the program is error free it compiles successfully but when I try to run it, it says
    Exception in thread “main” java.lang.UnsupportedClassVersionError: client : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: client. Program will exit.
    please help me out as how i can solve this.

    • Navya, you should java’s path in environment variable…
      Follow the following steps:
      Select Computer from the Start menu
      Choose System Properties from the context menu
      Click Advanced system settings > Advanced tab
      Click on Environment Variables, under System Variables, find PATH, and click on it.
      In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value.
      Reopen Command prompt window, and run your java code

    • When you change the environment variable with the JDK’s path. reload the command prompt and then type c:> javac -ver

      if you see the version of JDK. Then you are good to go

  9. Sir, can u plz suggest me a bestest Java project which I can make and can make a great impact on everyone by showing that project

  10. can i start learning java core even if i dont know C language or any basic concepts of c
    like even loops or syntex or even 0.% knowledge of c
    CAN YOU RECOMMEND ME TO START JAVA CORE ?????
    ?????
    >>

  11. Hi,

    I have installed java successfully and also defined the ClassPath and Path in the system variables settings as follows
    JAVA_HOME C:Program FilesJavajdk1.8.0_40
    CLASSPATH %JAVA_HOME%jrelib
    PATH %JAVA_HOME%bin

    while executing C:UsersUserDesktop>javac FirstProgramme.java
    C:UsersUserDesktop>java FirstProgramme
    Error: Could not find or load main class FirstProgramme

    Please help to fix this.

    Thanks.

  12. I am new to JAVA. While executing the said script it is showing the below error. Wht should I do further
    C:Program FilesJavajdk1.7.0_71bin>javac C:UsersRekhaDesktopjava1.j
    C:UsersRekhaDesktopjava1.java:3: error: illegal character: 8220
    System.out.println(ôHello JBTö);
    ^
    C:UsersRekhaDesktopjava1.java:3: error: ‘;’ expected
    System.out.println(ôHello JBTö);
    ^
    C:UsersRekhaDesktopjava1.java:3: error: illegal character: 8221
    System.out.println(ôHello JBTö);
    ^
    C:UsersRekhaDesktopjava1.java:3: error: not a statement
    System.out.println(ôHello JBTö);
    ^
    4 errors

  13. hi sir i have planning to try for job with 1 yr exp in java , i have sound knowledge on j2se,j2ee,hiberate is it enough or want to learn anything pleas help me

    • When I was trying to get new job, I viewed online job postings and read the requirements to know exactly what I should know. It allowed me to research/learn the things that I didn’t know, and I was able to get the job. Hope that helps you!

  14. You are the Best MR.Authors. You work hard to help people.Thanks a lot for you Mr.Author and

    Gautam.You are the Best…….

  15. i have NEWLY installed JDK1.8.0 and netBeans 8.0.2version
    Problem:i have run below program getting run time error my coding below
    import java.sql.*;
    import java.io.*;

    public class jdbc
    {
    public static void main(String args[]) throws IOException
    {

    try {
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
    Connection con = DriverManager.getConnection(“jdbc:odbc:ba”);
    Statement select = con.createStatement();
    ResultSet result = select.executeQuery(“select * from test”);

    System.out.println(“Got results:”);
    while(result.next()) { // process results one row at a time
    String val = result.getString(2);
    System.out.println(“User_Id = ” + val);

    }
    select.close();
    con.close();
    }
    catch( Exception e ) {
    System.out.println(“connecting error”);
    e.printStackTrace();
    }
    }

    }

    set path:JAVA_HOME=C:Program FilesJavajdk1.8.0_40
    PATH=C:Program FilesJavajdk1.8.0_40bin;

    while running time error meg like below
    connecting error
    java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at jdbc.main(jdbc.java:10)

    please advise how run the above program please advise me

  16. This is very good website..
    Sir, can you give me any idea about Asp.net Or Java is good for future..??

    • Hi Avi,

      You just need to understand eclipse have different build release for different languages.

      There is no specific version just pick the java specific build and it will work for Java.

      http://www.eclipse.org/downloads/

      Here you can see different build to download. Select only java specific to download for different platform(Windows/ Unix)

      Hope it help.

      Regards
      Vivekanand Gautam

  17. Hi Sir,
    Very good work but you try to explain about each and every word and line sir.
    I mean like what is use of using the word PUBLIC,ARGS,[]…..
    everything then everyone would come to know the use of the words written in java program.

    Thanks

  18. Thank you Sir but now i worked different way i have use on-lee PATH Sir I’m not use CLASSPTH. this is cure-ct way. tell me Sir.

    • i think u have to set CLASSPATH variable.. and that should refer to your file’s class path

      ex : dir/Firstprogram.java ….. after compilation ….. dir/Firstprogram.class

      CLASSPATH -> C:/dir

  19. Dear Vivekanand,

    Your site was recommended to us by out QA teacher in California. I love your site. Thank you for your time and hard work.

    Alec.

  20. Please tell i can’t understanding this kind of error, Program is ex cute but out put like this tell me Sir.

    C:UsersacerDocumants>javac FirstProgramme.java

    C:UsersacerDocumants>java FirstProgramme
    Error: Could not find or load main class FirstProgramme

  21. I think your page has the missing link. I couldnot find the page which contains the difference between JDK vs JRE …..

  22. Hi Sir

    I am a php developer and trying to get the basic java understanding, I am using Eclipse and create the java application .but its very terrible it to run in eclipse.
    Please Give direction For Eclipse

    Thanks
    Rahul

  23. Hi Admin,

    I followed your steps to install Java from the link provided. While setting the environment variables I saw that there are only bin and lib folders, there is no jdk folder. Please let me know why I might be facing this problem.

  24. This is good website to learn programming language …… thanks admin for supporting us to learn different programming language easily …..

  25. hiiii sir,

    Please help me i m the beginer and dont know anything about java plz send me the notes on my mail id..
    Thankyou.

  26. I know the java basic and im using netbeans , i want to learn run in core java means netbeans is enough or any other tool want to download ?

  27. Dear sir

    Please help me,

    I have installed the java two time as you instruct above but after successfully installed the software it shows the problem (‘java’ is not recognized as an internal or external command,
    operable program or batch file.)

    Please suggest me what should I do.

  28. Dear sir

    Please help me.

    I have installed the java software as to told at above, its installed successfully but when I test it then it shows the problem(‘java’ is not recognized as an internal or external command,
    operable program or batch file.)

    Please help suggest me what should I do.

    • You have to set the path variable for java..
      Right click on My Computer and select properties.
      Select advanced system settings.

      Click on environment variables tab.
      Now under user variables section , add a new variable with it’s path being the installation directory of java.

    • Try editing the Enviroment variables, make sure you include the semicolon before you write the path of where your JDK was installed, or it will not work. if you did install it in the same directory as the tutorial, your path should be:
      C:jdtbin

      and your path variable should look like (On the end of it):
      ;C:jdtbin

      if that doesn’t work, try making a folder in the jdt folder, that should work as long as you specify the directory.

    • Did you install the Java JDK and also check the version you have installed if the latest version is not supporting then try to install the lower version.

  29. hi
    u just stopped after clicking on the new button. what to after clicking on the new button, its showing two blanks areas what i have to enter

    • Hi Kavi,

      I think you are talking about Environment variable settings. When you click on New button a window will be opened. In that window provide the environment variable name and value for the same.

      Example
      Variable Name : JAVA_HOME
      Variable Value : C:Program FilesJavajdk1.7.0_15

      Hope this helps

      Thanks

  30. My teacher say make ur first love java … i think now its possible…this site is awesome to learn things in very deep..
    Thanku sir…

  31. i have successfully installed java and i have checked it also in cmd… then i wrote the program in notepad and saved it as .java (and file name same as in the 1st line of programme). then in the next step again in cmd to change the file destination when i am writing the given codes, its says error “cant specify path file”.. plz help me out.

  32. Hi,
    am begineer to this java. i have downloaded and installed the java s/w but i couldn’t find shortcut key to open and run the s/w. actually wats the problem with it? could you plz provide java programming tutorial?
    thank yu

  33. i have downloaded JRE7 from the link mentioned above .So, i want to know whether i have to download JDK or nor.

  34. Thanks to Mr. Vivekanand Gautam.
    I truly appreciate your time and effort to make this Website.. It’s wonderful & easy to learn 🙂

  35. The code is getting compiled but while running the .class file it is showing the following error.
    Please look into the matter.

    java.lang.ClassNotFoundException: FirstProgram
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    java.lang.ClassLoader.loadClass(Unknown Source)
    sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    java.lang.ClassLoader.loadClass(Unknown Source)

    • Hi Amriteya,

      If class file is create properly and java is not working then it might be the case that current folder is not in path(Enviornment variable) of your OS.
      Please check all you path, classpath in enviornment variable. It should contain . as . represent current folder in windows OS.

      For your reference you can check my path entries.

      .;%M2%;%JAVA_h%;C:Program FilesJavajre7lib

      Here i have added . so it will compile and run wihout any problem.

      Thanks

  36. Hi,

    Below is the code i was trying,same as u have mentioned ..pls check
    public class FirstProgramme {
    public static void main(String args[]) {
    System.out.println(“Hello JBT”);
    }
    }

    • Hi Babbu,

      I don’t see any issue with given code. I am able to compile and run the application without any problem.

      I hope you have executed below command in sequence

      javac FirstProgramme.java
      java FirstProgramme

      I am able to execute the command without any problem and output get displayed properly.

      Thanks

  37. Hi,

    when i type javac it gives me all the options properly and i cud see java -version also.after compiling i cud see a class file created in the folder.but when i type java FirstProgramme..it says ‘Could not find or load main class FirstProgramme..pls help me where im gng wrong

    • Hi babbu,

      There seems to be some problem with your source code. Java seems to be installed properly. Please share the source code i ll check and let you know what is the problem..

      Thanks

  38. hello sir,

    my programm doesnt compile.i was install jdk properly.i also set path but still its give me an error like ‘javac’ is not recognize as an internal or external command,
    operable programm or batch file.
    pls give suggestion

    • Hi Sneha,

      Sorry for the late reply. As you can system is throwing an error of knowing command. In simple way you can say that your operating system(Windows) doesn’t know where this javac command(path).

      So to make OS understand you have to add path of javac in path in enviornment variable in My Computer.

      To know how to set path in windows you need to search on google or you can search article written by me.

      Hope this help.

      Thanks

  39. kumud,
    i found error like ,Class names, “Example”, are accepted only if annotation processing is explicitly request
    please suggest me for this error

  40. where i can see my jdk file.? in bin folder i seen jre file. when i can set the path it shows javac is not
    recoginsed as an internal or external command. whats the reason? please tell me

    • Hi Kalpana,

      You must download and install JDK from Oracle website and not JRE. Once you install JDK folder instide JDK should be like below

      lib
      jre
      include
      db
      bin

      If everything is installed properly you should see javac executable(javac.exe) file in bin folder.
      If it is not therer then you have not installed JDK properly. If you confirm that JDK is installed properly then add path till bin to classpath /path in enviornment variable. Then only windows will be able to recognise this command.

      Thanks

  41. my javac not working when i run my program with javac it give statements javac is not recognized external &enternal then what should i do for compiling java program

    • Hi Garg,

      It seems like JDK is not installed in your system. Please install the same and check if javac is working or not(JDK and not JRE). If you have already installed then check if path for bin is already added in environment variable or not.

      It should resolve your problem.

      Thanks

  42. hey iam a beginner..i jus wanna learn from these ol tutorials..soo after writin a program n savin dat what should we do for compiling???i mean whatz d next step…???i immeditly writin d program i jus got to cmd n did what yu said but the error JBT cant be recognised by an internal or external command,z comin..so plz suggest me a solution admin…

    • Yes, You can develop or run Java application in any major platform including Linux. You just need to download JDK specific for Linux. And you can start developing application. Click Here for different versions of JDK version for different platform.
      You can also download Eclipse for Linux from here

      Thanks

  43. hey admin 🙂 its a rly cool site wanna know if this would help me to pass oracle jse 7? and anther thing if there any video tutorial ? to it thx alot 😀

  44. Hello Sir,
    I am a beginner of core java. Tutorials are helping me a lot. but can you tell me, Where can i have exercise for practice. How one can develop a logic in programming?
    Thanks..

    • Hi Kamlesh,

      Soon i will upload the list of assignment that you can take help from and start developing the code.

      Thanks

    • Hi Bharat,

      #1 You have to set path as C:j2sdk1.5.0_04bin and class path as
      C:j2sdk1.5.0_04lib
      #2 Open cmd and hit this target “java -version”, it shows
      java version “1.6.0_17”
      Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
      Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)
      #3 On cmd hit this target “javac”, it shows
      Usage: javac
      where possible options include:
      -g Generate all debugging info
      -g:none Generate no debugging info
      -g:{lines,vars,source} Generate only some debugging info
      -nowarn Generate no warnings
      ……………………………

      Now run the programme as per Above …………….
      Hope this is help you guys …………… 🙂

  45. problem solved just set(or type) in cmdpromt.. setpath= C:Program FilesJavajdk1.7.0_10bin or where are ever you saved your jdk file i hope thats help you

      • seting path is simple…
        follow these instrucitons in ur command prompt…
        1. cd
        2. cd java
        3. cd jdk1.6.0_03
        4. set path=c:javajdk1.6.0_03bin
        5. cd bin

        to enter any file inside ur bin folder follow this..

        6. cd foldername
        ex: cd threads

    • Hi,

      Thanks for your response. I will update the article asap. Meanwhile before you compile source code check if path of javac is in the class path. To check it execute the command “javac” in command prompt.
      If response is
      ‘javac’ is not recognized as an internal or external command,operable program or batch file.

      It means path is not set currectly. To set the pat go to enviornment variable section of computer properties.From there set the path of javac.”C:Program FilesJavajdk1.7.0_10bin”.
      Note this will be permanent and you don’t need to do the same thing of setting path again and again.

      Thanks Max again for pointing this.

  46. Hi get this error after run the command
    Error : could not find or load the main class FirstProgramme

    please answer.

    the same ^ just answer what should we do so in the future people not need to wait abswer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.