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

在Bash脚本中调用Docker内javac报错,请求排查提示

Let's figure out why your javac command is throwing that "invalid flag" error. Here are some practical troubleshooting steps and fixes to try:

1. Stop using source (.) to run the script

Your script has a #!/bin/bash shebang, which means it’s designed to run as a standalone executable, not sourced. Using . compile_java.sh ... runs the script in your current shell session, which can introduce unexpected behavior with parameter parsing or environment variables.

First, make the script executable:

chmod +x compile_java.sh

Then run it normally:

./compile_java.sh TestJava.java

2. Quote your argument in the Docker command

The most likely root cause here is unquoted parameter expansion. Even if your filename has no spaces, passing $1 without quotes can lead to odd parsing issues in the Docker context. Update your Docker command to wrap $1 in quotes:

docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:15 javac "$1"

Quoting ensures the filename is treated as a single, intact argument for javac.

3. Verify the target file exists

Double-check that TestJava.java is actually in your current working directory with:

ls -l TestJava.java

If the file doesn’t exist, you’ll need to switch to the correct directory or fix the filename you’re passing to the script.

4. Test the Docker command manually

To rule out script-specific issues, run the javac command directly in your terminal:

docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:15 javac TestJava.java

If this works, the problem lies in your script’s parameter handling. If it fails, you can debug the Docker setup itself (e.g., confirm the volume mount is working correctly).

5. Check for Windows line endings

If you wrote the script on a Windows machine and transferred it to Ubuntu, it might have Windows-style CRLF line endings instead of Unix LF endings. This can cause weird parsing errors in bash.

Check for this with:

cat -A compile_java.sh

If you see ^M at the end of lines, convert the script to Unix format with:

dos2unix compile_java.sh

6. Debug the parameter being passed

Add an extra echo line right before the Docker command to confirm exactly what’s being sent to javac:

echo "Executing javac with argument: '$1'"
docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp openjdk:15 javac "$1"

This will reveal any hidden characters or unexpected modifications to the filename that might be causing the error.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 16:37:40