修改Gradle Java约定目录结构后,.class文件生成位置查询
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
.classfiles: Navigate tobuild/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 iscom.example.MyApp, you'll find the file atcom/example/MyApp.class.Run the application:
Option 1: Use thejavacommand directly. Open a terminal in your project's root folder, then run:java -cp build/classes/java/main com.example.MyAppThe
-cpflag 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
runtask (easier!). First, add this configuration to yourbuild.gradle(Groovy DSL) to specify your main class:application { mainClass.set('com.example.MyApp') }Then just run this command in your terminal:
gradle runGradle will handle the classpath and execute your app automatically.
内容的提问来源于stack exchange,提问作者Varuna

