使用Apache Tika的主题建模程序导出JAR后执行报安全异常求助
java.lang.SecurityException: Prohibited package name: java.sql with Apache Tika JAR Export Hey there, let's break down what's going on here and get your standalone JAR working like it does in Eclipse.
First off, that security exception is Java's hard rule kicking in: it strictly prohibits user-created code from using packages that start with java.—those are reserved exclusively for the core Java runtime. The reason it works in Eclipse but fails in your exported JAR is because Eclipse uses a more flexible classloader setup that doesn't trigger this check, but when running the standalone JAR, the system classloader catches the forbidden package immediately.
Here are the most actionable fixes to try:
1. Fix Your Eclipse JAR Export Configuration
The #1 culprit here is almost always how you're packaging your JAR. If you're using Eclipse's "Runnable JAR File" export option, you might be accidentally bundling core Java classes or conflicting dependencies. Let's adjust that:
- Right-click your project → Export → Runnable JAR File
- Pick your main class from the "Launch configuration" dropdown
- Under "Library handling", select Copy required libraries into a sub-folder next to the generated JAR
- Finish the export, then make sure the auto-generated library folder stays in the same directory as your JAR when you run it with
java -jar your-app-name.jar
This keeps third-party dependencies (like Tika) separate from your code, avoiding clashes with core Java packages.
2. Double-Check for Accidental java.sql References
Even if you're sure your code is clean, take a quick pass:
- Do any of your custom handlers, parsers, or utility classes reference
java.sql? Make sure none of your classes are actually defined inside thejava.sqlpackage (that's the forbidden move). - Verify your Apache Tika version. Older Tika releases might have edge cases where certain file parsers load
java.sqlclasses in a way that triggers this error in standalone JARs. Try upgrading to the latest stable Tika version to rule this out.
3. Inspect Your Exported JAR's Contents
Unzip your exported JAR and look for a java/sql directory or any .class files under that path. If you find them, that's the problem—those core Java classes shouldn't be in your JAR. Adjust your export settings to exclude core libraries, or check if any of your dependencies are incorrectly bundling these classes (though Tika itself shouldn't do this).
4. Verify Your ParseContext Setup
Take a quick look at how you're configuring ParseContext before calling parser.parse(). Are you adding any custom components linked to java.sql? For example, a custom metadata handler that uses JDBC? If so, ensure those classes live in your own package names (not starting with java.).
Once you fix your packaging or eliminate any accidental forbidden package references, your standalone JAR should run smoothly just like it does in Eclipse.
内容的提问来源于stack exchange,提问作者Phong Nguyen

