编译CheckSum.java遇javac报错及picocli依赖缺失问题求助
Let’s work through your problems step by step to get your code compiling and running correctly:
1. Fixing the Invalid javac Command Syntax
Your first compilation command misused the -classpath option, which caused the "file not found" error:
javac -classpath "C:\PROJECTS FOR FOXMINDED\TASK 12 - Service Layer\src\main\java\com\foxminded\university\cli\CheckSum.java"
The -classpath (or -cp) flag tells javac where to find dependency classes, not which source file to compile. Since you didn’t specify a source file, javac tried to look for .java files in a default system path, hence the confusing error.
To compile your file correctly when you’re in its directory, you only need to run:
javac CheckSum.java
We’ll add the picocli dependency to the classpath in the next section.
2. Resolving the Picocli Package Not Found Errors
All 8 compilation errors happen because javac can’t locate the picocli library your code uses. Here’s how to fix this:
Step 1: Get the Picocli JAR File
Download the picocli JAR file (use the latest stable version available) and save it to a convenient location—for example, the same directory as your CheckSum.java file.
Step 2: Compile with the Picocli Classpath
When compiling, use the -cp option to include the picocli JAR in the classpath. If your JAR is named picocli-4.6.3.jar (adjust the version to match your download) and is in your current directory, run:
javac -cp picocli-4.6.3.jar CheckSum.java
If the JAR is in another folder, use its full path:
javac -cp "C:\path\to\your\picocli-4.6.3.jar" CheckSum.java
Step 3: Add Missing Import Statements
Your code uses several core Java classes without importing them, which will cause additional errors even after fixing the picocli issue. Add these imports at the top of your CheckSum.java file (before the picocli imports):
import java.io.File; import java.nio.file.Files; import java.security.MessageDigest; import java.math.BigInteger; import java.util.concurrent.Callable;
Step 4: Run the Compiled Program
After successful compilation, you need to include both the current directory (where your CheckSum.class file lives) and the picocli JAR in the classpath to run the program:
java -cp ".;picocli-4.6.3.jar" CheckSum
Note: On Windows, use ; to separate classpath entries; on Unix-like systems, use : instead.
3. A Quick Note on System CLASSPATH
Your current system CLASSPATH only points to the JDK’s lib directory, which doesn’t include the picocli JAR. It’s best practice to specify the classpath per command (as we did above) instead of modifying the system CLASSPATH, since this avoids conflicts with other Java projects.
内容的提问来源于stack exchange,提问作者user13566239

