You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改Gradle Java约定目录结构后,.class文件生成位置查询

Gradle Java Class File Location: Answers to Your Questions

Hey there, let's break down these common Gradle questions clearly—customizing source directory structures can make you wonder about build output paths, so I've got you covered!

1. Where do .class files go after modifying Gradle's Java convention directory structure?

Gradle keeps its build output locations separate from your source code by default, and changing your source directory structure doesn't automatically alter where .class files are generated.

No matter how you reconfigure your source sets, compiled .class files for the main codebase will land in the build/classes hierarchy. Specifically, for Java projects, this is almost always build/classes/java/main (if you're using Kotlin, it would be build/classes/kotlin/main instead). The build folder is Gradle's standard spot for all generated artifacts—so your source changes won't mess with that convention.

2. If I change the default src\main\java to src\java\main, where do .class files end up?

Same place as before: build/classes/java/main.

To confirm your custom source directory is set up correctly, here's what the configuration would look like in a Groovy-based build.gradle:

sourceSets {
    main {
        java {
            srcDirs = ['src/java/main']
        }
    }
}

Even with this custom source path, Gradle still compiles everything into the standard build/classes/java/main directory. This keeps your source code and build outputs cleanly separated.

3. After running gradle build and seeing BUILD SUCCESSFUL, how do I find and run the .class files?

Here's a step-by-step guide:

  • Locate the .class files: Navigate to build/classes/java/main (or the Kotlin equivalent if needed). Inside this folder, you'll see subdirectories matching your Java package structure. For example, if your main class is com.example.MyApp, you'll find the file at com/example/MyApp.class.

  • Run the application:
    Option 1: Use the java command directly. Open a terminal in your project's root folder, then run:

    java -cp build/classes/java/main com.example.MyApp
    

    The -cp flag sets the classpath to your compiled classes directory, and you specify the full package name of your main class.

    Option 2: Use Gradle's built-in run task (easier!). First, add this configuration to your build.gradle (Groovy DSL) to specify your main class:

    application {
        mainClass.set('com.example.MyApp')
    }
    

    Then just run this command in your terminal:

    gradle run
    

    Gradle will handle the classpath and execute your app automatically.


内容的提问来源于stack exchange,提问作者Varuna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:02:48