JIYIK CN >

Current Location:Home > Learning > PROGRAM > Java >

Compile multiple Java files with a single command in Java

Author:JIYIK Last Updated:2025/04/14 Views:

This tutorial explains how to compile multiple java files using a single command in Java.

Compilation is a term used to refer to the process of converting java source code into bytecode using JDK. To execute any Java file, we need to follow two steps: compile the code and then run the compiled code.

During the compilation process, JDK checks the code for correctness and syntax and generates a .classfile containing bytecodes that can only be read by JVM. Here, we will learn to compile single and multiple Java files using a single Java command.

So let's start by compiling the Java files first.


Compile a Java file

To understand how to compile multiple Java files using a single command, let us first understand how to compile a single Java file. Java provides a javac command to compile the source code and create a .classfile to compile a Java file.

Here, we have created a Hello class and Hello.javasaved the code with the name . To compile it, we have used javacthe command.

See the example below.

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

To compile the Java file, we have used the following command. This command will create a file in the current directory .class.

javac Hello.java

To run the file generated by the above command .class, we used the following javacommand, which uses JVM to execute the code.

java Hello

Output:

Hello

Compiling multiple Java files

First, create two java files Hello.java and Hello2.java and javaccompile them using command. We use the same command to compile multiple Java files by providing multiple Java file names.

See the example below.

document:Hello.java

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

document:Hello2.java

class Hello2 {
  public static void main(String[] args) {
    System.out.println("Hello from 2");
  }
}

To compile the above two Java files, we used the following command. This command will create two files in the same directory as Hello.classand .Hello2.class.class

javac Hello.java Hello2.java

Compile all Java files in the current directory

If we have multiple Java files in the current directory and want to compile all of them with a single Java command, we can use the following command. Here, we use *the wildcard to specify all the Java files.

javac *.java

If the Java files are in different directories, then use the following command. Here, we have specified the path of the directory and *to compile all the Java files under the specified directory.

See the command below.

javac / root / rohan / directoryname/*.java

Previous:Arrow operator in Java ->

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Arrow operator in Java ->

Publish Date:2025/04/14 Views:113 Category:Java

This tutorial explains - the role of the arrow operator ( ) in Java and lists some sample code to understand the topic. In Java 8, a new feature lambda expression was added, and the arrow operator appeared in Java to form lambda expressions

>> operator in Java

Publish Date:2025/04/14 Views:66 Category:Java

This guide will introduce you to the operator in Java . To understand this concept, you need to be familiar with some lower-level computing concepts. For example, bits, bytes, etc. Let's take a deeper look. Operators in Java In Java, the op

如何在 Java 中延迟几秒钟的时间

Publish Date:2023/12/17 Views:223 Category:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

Publish Date:2023/12/17 Views:190 Category:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

Publish Date:2023/12/17 Views:172 Category:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

Publish Date:2023/12/17 Views:198 Category:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

Publish Date:2023/12/17 Views:152 Category:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

Publish Date:2023/12/17 Views:84 Category:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

Publish Date:2023/12/17 Views:224 Category:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial