IntelliJ构建的JAR包无法包含InvalidArgumentException问题求助
InvalidArgumentException Issues in Your IntelliJ-Built JAR Hey Aaron, let's break down why you might be hitting issues with InvalidArgumentException even though your JAR builds and runs otherwise. Here are common culprits and actionable steps to debug:
Modular Project Configuration Gaps
If your project uses Java modules (has amodule-info.java), double-check that your module properly accesses thejava.basemodule (whereInvalidArgumentExceptionresides). Whilejava.baseis implicitly required for most projects, misconfigurations like restricted exports or accidental module restrictions could cause runtime access issues.Incomplete Artefact Packaging
Head back to your Artefact Structure settings in IntelliJ. Verify that the class file containing the code that throwsInvalidArgumentExceptionis actually included in the JAR output. You can manually unzip your built JAR and check if the corresponding.classfile exists in the correct directory structure. If it's missing, adjust your artefact to include the relevant module or source set.JDK Version Mismatch Between Build and Runtime
Even thoughInvalidArgumentExceptionhas existed since JDK 1.5, a mismatch between the JDK you used to build the JAR and the one you're using to run it could cause unexpected behavior—especially if you're using modular features. Runjava -versionto confirm the runtime JDK, and compare it to the Project SDK set in IntelliJ's Project Structure.Subtle Code Logic Issues
Sometimes the problem isn't with the exception itself, but how it's being thrown or handled. For example:// Check if the value triggering the exception has unexpected edge cases if (input < 0) { throw new IllegalArgumentException("Input cannot be negative: " + input); }Verify if the value you're passing when throwing the exception is causing unintended side effects, or if the exception is being swallowed incorrectly elsewhere in your code.
IntelliJ Cache Corruption
IDE caches can sometimes lead to mismatched builds. Try these steps to rule that out:- Go to
File > Invalidate Caches...and select "Invalidate and Restart" - Rebuild your entire project with
Build > Rebuild Project - Delete and re-create your artefact in
File > Project Structure > Artefactsbefore rebuilding the JAR
- Go to
To narrow this down further, it would help to share:
- The full stack trace of the exception you're seeing (this will pinpoint exactly where and why it's failing)
- The code snippet where you're throwing
InvalidArgumentException - If it's a modular project, the content of your
module-info.java - The exact command you're using to run the JAR
内容的提问来源于stack exchange,提问作者LordoftheVoid

