树莓派执行Gradle构建时出现「无法创建父目录」错误
Hey Simon, let's tackle these two issues one by one to get your Java app building smoothly on Raspberry Pi.
The reason your Gradle PATH only works in the local terminal is because the export command you ran only applies to the current shell session. Local and SSH sessions load different shell configuration files by default, so we need to make the PATH change permanent for all sessions of your simon user.
Here's how to fix it:
- Open your user's bash configuration file with a text editor (nano is easy for beginners):
nano ~/.bashrc - Scroll to the bottom of the file and add this line:
export PATH=$PATH:/opt/gradle/gradle-6.7/bin - Save the file (press
Ctrl+O, hit Enter, thenCtrl+Xto exit nano) - Apply the changes immediately to your current session:
source ~/.bashrc - Test it in a new SSH session: run
gradle -v—it should recognize the command without needing to set PATH manually now.
The core issue here is that your simon user doesn't have write permissions for the project directory /home/pi/Desktop/slotcar_project/SlotCar. Gradle needs to create the .gradle folder inside your project to store build caches, and right now it can't do that.
Let's fix this properly with one of two options:
Option 1: Grant write access to the pi group (since you added simon to this group)
If you want to keep the directory owned by pi but let simon modify it:
sudo chmod -R g+w /home/pi/Desktop/slotcar_project/SlotCar
The -R flag applies the permission change recursively to all files and subfolders in the project.
Option 2: Transfer directory ownership to simon (recommended for your development workflow)
This is the simpler long-term solution since you're developing as simon:
sudo chown -R simon:simon /home/pi/Desktop/slotcar_project/SlotCar
Now simon will have full read/write/execute access to the entire project directory.
Why sudo gradle build failed
When you use sudo, it runs commands as the root user, which doesn't inherit your simon user's PATH. If you ever need to run Gradle with sudo (though you shouldn't need to for your project), you could use:
sudo -E gradle build
The -E flag preserves your user's environment variables (including PATH) when running as root. But fixing the directory permissions for simon is the correct solution here.
After applying either permission fix, navigate back to your project directory and run gradle build again—it should complete without the directory creation error.
内容的提问来源于stack exchange,提问作者SimonA

