Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Java  Module 1 – Start here: JDK 25 LTS and your first programs

Classic main, javac and .class files

Write the classic public static void main(String[] args), compile it with javac -d, run classes from the class path, and see which main JDK 25 picks.

  • Beginner
  • 15 minutes
  • Examples run with Java 25.0.4.1 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 15 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Write the classic public static void main(String[] args) entry point and explain each keyword
  • Compile with javac -d and run a class from the class path with command-line arguments
  • Run a class in a package by its full name from the folders javac builds
  • Explain how the JDK 25 launcher chooses among main methods, and when the classic form is needed

Before you start

What you’ll learn · Before you start

What you will learn

  • Write the classic public static void main(String[] args) entry point and explain each keyword
  • Compile with javac -d and run a class from the class path with command-line arguments
  • Run a class in a package by its full name from the folders javac builds
  • Explain how the JDK 25 launcher chooses among main methods, and when the classic form is needed

Before you start

On this page

Before JDK 25, every Java program started from the same method declaration, public static void main(String[] args), usually in a public class named after its file. That classic form still runs on every JDK, it is how almost all existing code and textbooks start a program, and it is what online judges compile. This lesson explains each word of it, then the two-step way of running it: javac -d out Hello.java compiles the source into a class file in the folder out, and java -cp out Hello runs that class from the class path. It ends with the rules that the JDK 25 launcher follows when a class offers more than one main method.

Every word of the classic main has a job

Here is a classic program. It does one thing more than the compact files of the last lesson: it prints the arguments given to it on the command line.

Hello.java Java · Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from a classic main");
        System.out.println("Arguments received: " + args.length);
        for (String arg : args) {
            System.out.println("  " + arg);
        }
    }
}

Read the two declarations word by word:

  • public class Hello declares a class named Hello that code anywhere may use. A public top-level class must live in a file with the same name, here Hello.java.
  • public, in front of main, lets the launcher, which is outside the class, call the method.
  • static makes the method belong to the class itself, so it can be called without creating an object first.
  • void says that the method returns no value.
  • main is the name the launcher looks for.
  • String[] args is the one parameter: an array of strings that holds the command-line arguments: The words typed after the class name, as in java -cp out Hello Asha Pune. The launcher passes them to main as the array String[] args, which is empty, never null, when there are none., and is empty when there are none.

System.out.println is the classic way to print a line: System.out is the standard output stream, and println writes a value and ends the line. It does the same job as IO.println, and you will meet both. The for loop prints each argument in turn; module 3 explains loops. Step through one launch of this class with two arguments, and see what the launcher needs each word for:

Interactive · message by message One launch of the classic main, word by word Step through java -cp out Hello Asha Pune. Each step shows what the launcher needs one word of the signature for.
One launch of the classic main, word by word, step 7 of 7. Messages so far: Terminal to The launcher: Hello Asha Pune; The launcher to class Hello: args: Asha, Pune; class Hello to Terminal: Arguments received: 2.Terminalwhere you typeThe launcherjava starts a JVMclass Helloout/Hello.classHello Asha Puneargs: Asha, PuneArguments received: 2

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. The command. You type java -cp out Hello Asha Pune. The class to run is Hello, and the two words after its name are arguments for the program.
  2. Find the class. The launcher finds out/Hello.class on the class path out. The next section shows the javac command that wrote it.
  3. public. It looks for public static void main(String[] args). public lets code outside the class call the method, and the launcher is outside the class.
  4. static. static makes main belong to the class itself, so the launcher can call it without creating an object of Hello first.
  5. String[] args. The words after the class name become the array args, here the two strings Asha and Pune. With no words after the name it would be an empty array, never null.
  6. main runs. main prints Hello from a classic main, then Arguments received 2, and then each argument on a line of its own.
  7. void. void means that main gives back no value. When it returns, the program ends; a program reports a failure through an exception or System.exit, not through a return value.

A public class needs a file of its own name

The compiler insists on the first rule of the list above. This file declares a public class Greeting but is called Mismatch.java:

Mismatch.java Java · Mismatch.java
// A public class in a file with another name.
public class Greeting {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}
javac checks the file name Python · file_name_session.py
from session import run

run("javac -d out Mismatch.java", expect_failure=["javac -d out Mismatch.java"])

Output

# JDK in use: openjdk version "25.0.4.1" LTS
$ javac -d out Mismatch.java
Mismatch.java:2: error: class Greeting is public, should be declared in a file named Greeting.java
public class Greeting {
       ^
1 error
[exit status 1]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 file_name_session.py

The Java Language Specification allows a compiler that stores code in files to enforce this, and javac does, which is why every public class lives in a file of its own named after it. Classes that are not public may share a file.

Checkpoint

Check: The words of the classic main

  1. Question 1 of 2 Why is the classic main declared public?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: So that the launcher, which is outside the class, may call it

    public, static, void, main and String[] args each have a job. public is the one that lets the launcher, which is outside the class, call main.

  2. Question 2 of 2 A file named Shop.java declares public class Store. What does javac do with it?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: It refuses, because a public class must be declared in a file named Store.java

    The Java Language Specification lets a compiler that stores code in files insist that a public top-level class lives in a file of its own name, and javac does. Rename the file, or drop public if the class may share a file.

javac writes a class file once, and java runs it from the class path

The source launcher of the last lesson compiles in memory every time. For anything you run more than once, compile once with javac, keep the class file, and run it as often as you like:

Two routes from Hello.java to a running program: java Hello.java compiles in memory, or javac writes out/Hello.class for java -cp out.Source-file mode: one commandClass-file mode: two commandsHello.javajava Hello.javacompiles in memory, then runsThe program runs;no class file is keptHello.javajavac -d out Hello.javaout/Hello.classkept on diskjava -cp out Hellofinds the class on the class pathThe program runs;run it again without compiling

Source-file mode and class-file mode

Text description of the diagram

The diagram shows two routes from the same source file, Hello.java, to a running program, one above the other.

  1. Source-file mode, one command: java Hello.java compiles the file in memory and runs it at once. No class file is kept.
  2. Class-file mode, two commands: javac -d out Hello.java writes the class file out/Hello.class, which stays on disk; then java -cp out Hello finds the class on the class path and runs it. The class file can be run again without compiling.
javac -d out, then java -cp out Python · hello_session.py
from session import run

# Compile once into out, then run the class three times with different arguments.
run("""
javac -d out Hello.java
find out -type f
java -cp out Hello
java -cp out Hello Asha Pune
java -cp out Hello "New Delhi"
""")

Output

# JDK in use: openjdk version "25.0.4.1" LTS
$ javac -d out Hello.java
$ find out -type f
out/Hello.class
$ java -cp out Hello
Hello from a classic main
Arguments received: 0
$ java -cp out Hello Asha Pune
Hello from a classic main
Arguments received: 2
  Asha
  Pune
$ java -cp out Hello "New Delhi"
Hello from a classic main
Arguments received: 1
  New Delhi

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 hello_session.py

  • javac -d out Hello.java compiles the file and writes the class into the folder out, creating the folder if needed. Silence means success.
  • java -cp out Hello runs it. -cp, short for --class-path, lists where the JVM should look for classes, the class path: The folders and JAR files in which the JVM looks for classes, given with -cp or --class-path. Without it, and without a CLASSPATH environment variable, it is the current folder.; without it (and without a CLASSPATH environment variable), the class path is the current folder. Notice that you name the class, Hello, not the file: no .java and no .class.
  • Words after the class name become the elements of args. Quotes keep "New Delhi" together as one argument, and with no arguments args.length is 0, not an error.
Hex Viewer & File Inspector Open out/Hello.class and find the magic number CA FE BA BE and the class file version 69 (0x45).

Key idea

javac -d out Hello.java compiles once into out/Hello.class, and java -cp out Hello runs the class, named without .java or .class, as often as you like.

Checkpoint

Check: javac, the class path and arguments

  1. Question 1 of 2 You run java Hello with no -cp option and no CLASSPATH variable set. Where does the JVM look for Hello.class?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: In the current folder

    -cp, short for --class-path, lists where the JVM looks for classes. Without it, and without a CLASSPATH environment variable, the class path is the current folder.

  2. Question 2 of 2 How many elements does args have when the program runs as java -cp out Hello "New Delhi" Pune?

    Type a number.

    A hint

    Quotes keep the words between them together.

    Show the answer: Question 2 of 2

    Answer: 2 elements

    The quotes keep New Delhi together as one argument, so args holds two strings, New Delhi and Pune. The session's run with "New Delhi" alone printed Arguments received 1.

A class in a package runs by its full name

Real programs put their classes in package: A named group of classes, such as com.example.shop, declared on the first line of a source file. javac -d stores its class files in folders that follow the name, and java runs such a class by its full name., named groups such as com.example.shop, declared on the first line of the file. javac -d then builds a folder for each part of the package name, and java needs the class’s full name:

src/com/example/shop/App.java Java · src/com/example/shop/App.java
package com.example.shop;

public class App {
    public static void main(String[] args) {
        System.out.println("Running " + App.class.getName());
    }
}
A class in a package Python · packages_session.py
from session import run

# A class in a package: javac -d builds the matching folders, and java needs the class's full name.
run("""
javac -d out src/com/example/shop/App.java
find out -type f
java -cp out com.example.shop.App
java -cp out App
""", expect_failure=["java -cp out App"])

Output

# JDK in use: openjdk version "25.0.4.1" LTS
$ javac -d out src/com/example/shop/App.java
$ find out -type f
out/com/example/shop/App.class
$ java -cp out com.example.shop.App
Running com.example.shop.App
$ java -cp out App
Error: Could not find or load main class App
Caused by: java.lang.ClassNotFoundException: App
[exit status 1]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 packages_session.py

The class file landed in out/com/example/shop/App.class, and the class’s full name, com.example.shop.App, ran it. The short name App fails with Could not find or load main class App, because the class path folder out holds no App.class at its top. Step through the same session, from the package name to the folders and back:

Interactive · step by step From a package name to folders, and back to a full name Step through the session above. Watch how com.example.shop becomes folders, and which name finds the class again.
From a package name to folders, and back to a full name, step 5 of 5. Showing src/com/example/shop/, out/com/example/shop/, java -cp out, java -cp out App (ClassNotFoundException). In focus: src/com/example/shop/.javac -d outfull nameshort namesrc/com/example/shop/App.javaout/com/example/shop/App.classjava -cp outcom.example.shop.Appjava -cp out AppClassNotFoundException

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. The source. App.java starts with package com.example.shop; and sits in matching folders under src, the usual layout of Java projects.
  2. javac -d out. javac -d out builds one folder for each part of the package name and writes the class file into the last one, out/com/example/shop/App.class, as find showed.
  3. The full name. java -cp out com.example.shop.App names the class by its full name, so the JVM looks in out/com/example/shop/ and finds App.class. The program prints Running com.example.shop.App.
  4. The short name. java -cp out App looks for out/App.class at the top of the class path. There is none, so it fails with Could not find or load main class App.
  5. Build tools. Maven and Gradle use the same idea, with the package folders under src/main/java instead of src.

The source file sits in matching folders under src too, the usual layout of Java projects; build tools such as Maven and Gradle use the same idea, with the package folders under src/main/java.

Checkpoint

Check: Classes in packages

  1. Question 1 of 2 Which command runs the class whose file is out/com/example/shop/App.class?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: java -cp out com.example.shop.App

    A class in a package runs by its full name, com.example.shop.App, and the class path names the folder where the package folders begin, out.

  2. Question 2 of 2 java -cp out App failed with ClassNotFoundException: App. Which file did the JVM look for?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: out/App.class, at the top of the class path

    The JVM turns the class name into a path below each folder of the class path. App becomes out/App.class, which does not exist; com.example.shop.App becomes out/com/example/shop/App.class, which does.

JDK 25 picks main by its parameters, and older JDKs want the classic one

Since JDK 25, the launcher accepts more kinds of main method than the classic one, which is what made compact source files possible. The Java Language Specification (§12.1.4) and JEP 512 give the rules:

  1. A method qualifies when it is named main, returns void, is not private, and has either one String[] parameter or none. It may be static or an instance method, and it no longer has to be public.
  2. If the class has a qualifying main with a String[] parameter, the launcher chooses that one; only when there is none does it choose a main without parameters.
  3. A static method is called directly. For an instance method, the launcher first creates an object of the class, which needs a constructor without parameters that is not private, and then calls main on it.

This class offers both kinds, in the less obvious combination:

Shop.java Java · Shop.java
// Two candidate main methods in one class: which one does the launcher choose?
class Shop {
    static void main() {
        System.out.println("static main() without parameters");
    }

    void main(String[] args) {
        System.out.println("instance main(String[]) with " + args.length + " argument(s)");
    }
}
Which main runs, on JDK 25 and on JDK 21 Python · which_main_session.py
from session import run

# JDK 25 picks the main that takes String[]; JDK 21 accepts only public static void main(String[]).
run("""
javac -d out Shop.java
java -cp out Shop one two
javac --release 21 -d out21 Shop.java
""")
run("java -cp out21 Shop", jdk="21", expect_failure=["java -cp out21 Shop"])

Output

# JDK in use: openjdk version "25.0.4.1" LTS
$ javac -d out Shop.java
$ java -cp out Shop one two
instance main(String[]) with 2 argument(s)
$ javac --release 21 -d out21 Shop.java
# JDK in use: openjdk version "21.0.12.1" LTS
$ java -cp out21 Shop
Error: Main method not found in class Shop, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
[exit status 1]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 which_main_session.py

JDK 25 ran the instance method main(String[]), even though the other main is static: the parameter list decides, not the word static. The same class compiled for Java 21 and run on JDK 21 does not start at all, because before JDK 25 the launcher accepted only public static void main(String[] args) and says so in its error message. So when code must also run on older JDKs, write the classic signature. Step through the three rules on Shop:

Interactive · step by step How the JDK 25 launcher chooses a main method in Shop Step through the three rules on Shop's two main methods, then the same class on JDK 21.
How the JDK 25 launcher chooses a main method in Shop, step 6 of 6. Showing class Shop, static void main(), void main(String[]) (ran with 2 arguments), JDK 25 launcher, new Shop(), JDK 21 launcher (Main method not found). In focus: JDK 21 launcher.choosescreatesclass Shoptwo main methodsstatic void main()no parametersvoid main(String[])ran with 2 argumentsJDK 25 launcherthree rulesnew Shop()an object for mainJDK 21 launcherMain method not found

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. Two candidates. Shop declares two main methods. Both qualify under rule 1, because each is named main, returns void, is not private and has one String[] parameter or none.
  2. String[] wins. Rule 2 chooses a main with a String[] parameter whenever there is one, static or not. The JDK 25 launcher therefore takes void main(String[] args).
  3. An object first. Rule 3 says how to call it. This main is an instance method, so the launcher first creates an object of Shop, which needs a constructor without parameters that is not private; Shop has the default one.
  4. main runs. The launcher calls main on that object with the two arguments one and two, and the program prints instance main(String[]) with 2 argument(s).
  5. The static main waits. static void main() never ran. The parameter list decided the choice, not the word static; a main without parameters is chosen only when no main takes String[].
  6. On JDK 21. The same class compiled for Java 21 does not start on JDK 21. Before JDK 25 the launcher accepted only public static void main(String[] args), and its error message says so.

Almost every Java program written before JDK 25 starts from the classic main, and that is most of the Java you will read. The systems that judge programs automatically work with it too: they compile your file with javac and run the class with java, exactly as above. The online judge Kattis, for example, documents that it compiles Java with javac 25.0.3 and runs your main class from the class path, and the ICPC World Finals programming environment lists OpenJDK 21.0.4, a JDK on which a compact source file does not compile.

For coding rounds

Use the classic form, a public class whose name matches the file with public static void main(String[] args), unless the platform’s own instructions say otherwise.

Checkpoint

Check: Which main the launcher chooses

  1. Question 1 of 2 A class has only one main, void main(), an instance method without parameters. What does the JDK 25 launcher do?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: It creates an object of the class and calls main() on it

    The method qualifies under rule 1, it is chosen under rule 2 because no main takes String[], and because it is an instance method, rule 3 creates an object of the class before calling it.

  2. Question 2 of 2 A class has an instance method void main(String[] args), and its only constructor is private Shop(). What happens on JDK 25?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: The program does not start, because the launcher cannot create the object that main needs

    For an instance main the launcher first creates an object, through a constructor without parameters that is not private. A class whose only such constructor is private gives it no way to do that, so main never runs.

Practise: join the arguments

Complete Main.java so that it prints its command-line arguments on one line, joined by |, or the text (no arguments) when it receives none. With the arguments one two it prints one | two; with "New Delhi" Pune it prints New Delhi | Pune.

Main.java: the exercise Java · Main.java
// Exercise: print the command-line arguments joined by " | ", or "(no arguments)" when there are none.
// Compile with: javac -d out Main.java    Run with: java -cp out Main one two
public class Main {
    public static void main(String[] args) {
        System.out.println("TODO");
    }
}

The session compiles the class and runs three sample tests, each comparing the output with the expected line through diff, as in the last lesson’s exercise. As given, main prints TODO, so all three fail:

The sample tests before you start Python · main_exercise_session.py
from session import run

# The sample tests: run the class with three argument lists and compare each output with the expected line.
run("""
javac -d out Main.java
java -cp out Main > got.txt; diff expected-none.txt got.txt && echo PASS
java -cp out Main one two > got.txt; diff expected-two.txt got.txt && echo PASS
java -cp out Main "New Delhi" Pune > got.txt; diff expected-space.txt got.txt && echo PASS
""", expect_failure=[
    "java -cp out Main > got.txt; diff expected-none.txt got.txt && echo PASS",
    "java -cp out Main one two > got.txt; diff expected-two.txt got.txt && echo PASS",
    'java -cp out Main "New Delhi" Pune > got.txt; diff expected-space.txt got.txt && echo PASS',
])

Output

# JDK in use: openjdk version "25.0.4.1" LTS
$ javac -d out Main.java
$ java -cp out Main > got.txt; diff expected-none.txt got.txt && echo PASS
1c1
< (no arguments)
---
> TODO
[exit status 1]
$ java -cp out Main one two > got.txt; diff expected-two.txt got.txt && echo PASS
1c1
< one | two
---
> TODO
[exit status 1]
$ java -cp out Main "New Delhi" Pune > got.txt; diff expected-space.txt got.txt && echo PASS
1c1
< New Delhi | Pune
---
> TODO
[exit status 1]

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 main_exercise_session.py

First hint: String.join(" | ", args) joins the elements of an array with a separator, and args.length == 0 is true when there are no arguments. Remember to compile again with javac after every change, or the old class file runs.

Once all three tests pass, test yourself on the four ideas of this lesson. Each answer you give is explained on the spot, and the next question comes from the idea where you need it most:

Practice that adapts to you

Practise this lesson

11 questions about 4 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 11 In public static void main(String[] args), what does static mean?

    Choose one answer.

    Show the answer: Question 1 of 11

    Answer: The method belongs to the class, so it can run before any object of the class exists

    A static method belongs to the class itself. That is why the classic launcher could call main without creating an object first.

  2. Question 2 of 11 Why does javac reject a file Mismatch.java that declares public class Greeting?

    Choose one answer.

    Show the answer: Question 2 of 11

    Answer: A public top-level class must be declared in a file named after it, here Greeting.java

    The Java Language Specification lets a compiler that stores code in files enforce this rule, and javac does: class Greeting is public, should be declared in a file named Greeting.java.

  3. Question 3 of 11 Which change makes javac accept Mismatch.java, which declares public class Greeting?

    Choose one answer.

    Show the answer: Question 3 of 11

    Answer: Rename the file to Greeting.java

    Rename the file after its public class, or drop public so that the class may share a file with another name. Every public class lives in a file of its own named after it.

  4. Question 4 of 11 After javac -d out Hello.java, which command runs the program?

    Choose one answer.

    Show the answer: Question 4 of 11

    Answer: java -cp out Hello

    java takes the name of a class, without .class, and -cp out tells it to look for classes in the folder out. A file name ending in .java would switch to source-file mode instead.

  5. Question 5 of 11 The program Hello is run as java -cp out Hello with nothing after the class name. What is args.length?

    Type a number.

    Show the answer: Question 5 of 11

    Answer: 0

    args is an empty array when there are no command-line arguments, never null, so args.length is 0 and the session printed Arguments received: 0.

  6. Question 6 of 11 You change a line of Hello.java and run java -cp out Hello again without running javac. What runs?

    Choose one answer.

    Show the answer: Question 6 of 11

    Answer: The old class file, so the program behaves as before the change

    A class file stays as javac wrote it. After every change to the source, compile again with javac, or the old class file runs, which is what the exercise's hint warns about.

  7. Question 7 of 11 Where does javac -d out put the class file of com.example.shop.App?

    Choose one answer.

    Show the answer: Question 7 of 11

    Answer: out/com/example/shop/App.class

    javac -d builds a folder for each part of the package name below the folder it is given, so the class file of com.example.shop.App is out/com/example/shop/App.class.

  8. Question 8 of 11 App.java starts with package com.example.shop; and was compiled with javac -d out. Why does java -cp out App fail?

    Choose one answer.

    Show the answer: Question 8 of 11

    Answer: The class's full name is com.example.shop.App, and its file is in out/com/example/shop/

    A class in a package runs by its full name, and javac -d stores it in folders that follow the package name. java -cp out com.example.shop.App finds it.

  9. Question 9 of 11 Which of these methods qualify as a main method for the JDK 25 launcher?

    Choose every answer that is right.

    Show the answer: Question 9 of 11

    Answer:

    • void main()
    • static void main(String[] args)

    Rule 1: the method is named main, returns void, is not private, and has one String[] parameter or none. It may be static or an instance method, and it no longer has to be public.

  10. Question 10 of 11 A class declares static void main() and void main(String[] args). Which one does the JDK 25 launcher run?

    Choose one answer.

    Show the answer: Question 10 of 11

    Answer: void main(String[] args), because a main with a String[] parameter is chosen first

    JLS 12.1.4 prefers a candidate with a single String[] parameter; whether it is static only decides how it is called. Two candidates in one class are not an error.

  11. Question 11 of 11 A judge's documentation lists OpenJDK 21.0.4. Which entry point should your submission use?

    Choose one answer.

    Show the answer: Question 11 of 11

    Answer: public static void main(String[] args) in a public class whose name matches the file

    On JDK 21, as on every JDK before 25, the launcher accepts only the classic signature, and javac insists that a public class lives in a file of its own name. That form also runs on JDK 25 and later.

Interview questions

Fresher · What does each word of public static void main(String[] args) mean?

public lets code outside the class, including the launcher, call the method. static makes it belong to the class rather than to an object, so it can run before any object exists. void means it returns nothing; a program reports failure through an exception or System.exit, not a return value. main is the name the launcher searches for, and String[] args receives the command-line arguments, an empty array when there are none. A good answer adds what JDK 25 changed: under JEP 512 the launcher also accepts main methods that are not public or static or have no parameters, preferring one with a String[] parameter. Older JDKs, and many judges, still expect exactly this classic signature.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Fresher to mid

    A program runs fine with "java Booking.java". After "javac -d out Booking.java" it either fails to compile or, once compiled, "java -cp out Booking" refuses to start. Give the likely causes of each failure and explain why the source launcher let them through.

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Fresher to mid

    Your Java solution passes every sample on your laptop, but an online judge or a placement test platform reports a compilation error or a runtime error for it. Which Java-specific causes do you check first, and in what order?

    Model answer, Pro. First step and follow-ups

Key takeaways

  • The classic entry point is public static void main(String[] args) in a class, and a public top-level class must be in a file of the same name.
  • javac -d out Hello.java writes out/Hello.class, and java -cp out Hello runs the class, named without an extension, with args holding the command-line arguments, empty and never null when there are none.
  • A class in a package lands in matching folders and runs by its full name, such as com.example.shop.App.
  • JDK 25 chooses a main with a String[] parameter over one without, static or not, while JDKs before 25, and judges that run them, accept only the classic signature.

Terms in this lesson

class path
The folders and JAR files in which the JVM looks for classes, given with -cp or --class-path. Without it, and without a CLASSPATH environment variable, it is the current folder.
command-line arguments
The words typed after the class name, as in java -cp out Hello Asha Pune. The launcher passes them to main as the array String[] args, which is empty, never null, when there are none.
package
A named group of classes, such as com.example.shop, declared on the first line of a source file. javac -d stores its class files in folders that follow the name, and java runs such a class by its full name.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.