Java Engineering / JAVA FUNDAMENTALS / JAV-001

Build Doctor

Explore concept graph

Materi

Problem: aplikasi Java berjalan di IDE, tetapi gagal saat mvn clean verify atau java -jar di terminal. Kita perlu membuktikan penyebabnya, bukan sekadar mengganti versi dependency secara acak.

Mulai: jalankan ketiga perintah berikut pada terminal yang sama.

java -version
javac -version
mvn -version

Bandingkan JDK yang menjalankan Maven dengan javac. Jika berbeda, periksa JAVA_HOME, PATH, dan Maven toolchains sebelum menyentuh program.

Peta konsep statis. Ikuti alur teks dari atas ke bawah.
JAVA_HOME + JDK 21
       |
       v
Maven + dependency scopes ----> compile classpath
       |                               |
  clean / verify                    javac --release 21
       |                               |
       +---- compile -> test -> package -> verify
                                  |
                              .class + JAR
                                  |
                 Main-Class + runtime classpath
                                  |
                              java -jar
                                  |
                         observable application

Error pertama menentukan lapisan

Observasi Fase Pemeriksaan pertama
cannot find symbol Compile Source/import dan compile classpath
invalid target release: 21 Compile/toolchain JDK yang digunakan oleh Maven
no main manifest attribute JAR launch META-INF/MANIFEST.MF dan Main-Class
NoClassDefFoundError Runtime class loading JAR dan runtime classpath
mvn clean verify sukses, java -jar gagal Build vs distribution Verifikasi executable secara terpisah

Contoh konfigurasi pom.xml untuk proyek Java 21 sederhana, menggunakan main class dev.cs101.Main yang benar-benar ada:

<properties>
  <maven.compiler.release>21</maven.compiler.release>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.16.0</version>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.4.2</version>
      <configuration>
        <archive><manifest><mainClass>dev.cs101.Main</mainClass></manifest></archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Itu adalah fragmen yang harus berada di dalam elemen <project> POM lengkap. Pastikan nama kelas, groupId, artifactId dan versi sesuai proyek. Plugin JAR ini tidak otomatis menghasilkan fat JAR dengan semua library; jangan mengasumsikan dependency eksternal sudah tertanam.

Konsep 1 / 3

Identify the Java 21 JDK, javac and java runtime and distinguish a compile-time missing symbol from runtime class loading.

An IDE can use a different Java installation from Maven CLI, causing reproducible builds to fail on CI.

Mulai dari sini

Run java -version, javac -version and mvn -version in one terminal.

Intuisi utama

The JDK contains javac and the java launcher. javac resolves names and emits .class files; the JVM loads available classes during execution.

Run java -version, javac -version and mvn -version together. Maven reports its own active JDK, which may differ from the shell PATH and IDE. A missing type at compile time is diagnosed by javac before an executable exists. NoClassDefFoundError can occur later when a compiled class references another class missing from the runtime classpath. Fix the phase where the failure actually occurs.

Contoh bertahap

An IDE build works but Maven CLI reports incompatible Java or cannot find a type.

  1. Compare java -version, javac -version and mvn -version.

    Mengapa: The three commands reveal which runtime/compiler Maven actually uses.

    Hasil yang diharapkan: Record compatible Java 21 versions and Maven's JDK path.

  2. Read the first error from mvn clean verify.

    Mengapa: A missing compile-time type differs from runtime class-loading failure.

    Hasil yang diharapkan: An unresolved type blocks compilation before the program starts.

  3. Repair JAVA_HOME or the declared compile dependency and rerun from a clean terminal.

    Mengapa: Remove hidden IDE classpath differences.

    Hasil yang diharapkan: The same error does not recur from an independently invoked Maven build.

Konsep 2 / 3

Explain compile, runtime, test and provided Maven dependency scope using failure evidence.

A dependency with the wrong scope can make source fail compilation or let a successful compile fail at runtime.

Mulai dari sini

Write down whether a test-scoped dependency is available when compiling src/main/java.

Intuisi utama

Maven scopes choose which classpath contains a library; compilation and execution need different sets of classes.

compile is available to main compilation and runtime. test is available for tests, but not main compilation. runtime is present at execution and tests but absent from main compilation. provided supports compilation but assumes the container/JDK supplies the class at runtime. An ordinary JAR does not automatically bundle all resolved Maven dependencies. Use an error message, the effective POM and mvn dependency:tree to reason about what is missing.

Contoh bertahap

The application imports a class in src/main/java but its Maven dependency is marked test.

  1. Examine the dependency's scope in pom.xml.

    Mengapa: Main code cannot compile against a test-only dependency.

    Hasil yang diharapkan: Compile-time symbol resolution fails if the class is not otherwise available.

  2. Change the scope to compile only when main production code needs that class.

    Mengapa: Place the library on the right classpath.

    Hasil yang diharapkan: mvn clean verify now compiles.

  3. Run the packaged application and check runtime libraries separately.

    Mengapa: Compilation does not prove JAR packaging or runtime classpath.

    Hasil yang diharapkan: An absent runtime dependency can cause a class-loading error after a green build.

Konsep 3 / 3

Verify a Java 21 Maven build and a runnable executable JAR without relying on IDE state.

A green Maven build is not yet proof that java -jar can launch a distributable application.

Mulai dari sini

Explain why a compiled class may exist while java -jar cannot start an application.

Intuisi utama

mvn clean verify proves lifecycle checks; java -jar separately proves manifest Main-Class and runtime class accessibility.

Pin Maven build plugin versions and configure maven.compiler.release=21. A standard executable JAR requires Main-Class in its manifest. Run mvn clean verify from a clean shell, then execute the resulting artifact with java -jar. The regular JAR plugin does not automatically include all external runtime dependencies; choose a JDK-only first lab or explicitly package/resolve external classes. The included Java 21 fixture proves a native compiler failure, a missing runtime class, then recovery, but does not execute Maven or java -jar.

Contoh bertahap

mvn clean verify succeeds but java -jar says no main manifest attribute or fails to load a runtime library.

  1. Set maven.compiler.release=21 and configure the JAR Main-Class.

    Mengapa: The class format and executable entrypoint are independent contracts.

    Hasil yang diharapkan: pom.xml points to the intended Java release and actual main class.

  2. Run mvn clean verify twice without IDE.

    Mengapa: A clean build should not rely on stale output or IDE launch configuration.

    Hasil yang diharapkan: Both runs produce the same expected runnable artifact.

  3. Run java -jar target/your-artifact.jar and record stdout.

    Mengapa: A successful package step alone does not check the runtime.

    Hasil yang diharapkan: The app starts, or the exact error points to manifest or runtime classpath.

Sumber dokumentasi utama

Sumber

Cross-domain connections

Curated relationships for this unit at G1. Proposed edges are suggestions, not prerequisites.

Concept connectionsG1 · 2 visible concepts · 2 curated links
100%

Dashed = proposed relationship; solid = reviewed connection. Only reviewed prerequisites constrain a learning path. Keyboard and text links remain available.