“java.lang.SecurityException:禁止的包名:java.sql”错误仅在Eclipse

时间:2018-03-14 20:23:26

标签: java eclipse apache-tika

我正在使用Apache Tika编写一个主题建模程序,以从其他文件类型中提取文本内容。实际上它在Eclipse上完美运行。但是当我从Window 10的命令提示符导出到JAR文件时使用。当它尝试代码时出现此错误消息:“parser.parse(stream,handler,metadata,parseContext);”

“java.lang.SecurityException:禁止的包名:java.sql”

我没有在这里上传我的java代码,因为我不认为它们是问题的根源。因为它在Eclipse IDE中完美运行。所以有人知道为什么它只会在我尝试从命令行运行时发生。 Eclipse IDE内部和外部的JVM有什么不同?谢谢。

        package Views;

import java.io.*;
import org.apache.commons.io.FileUtils;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.BodyContentHandler;

public class TestTika {

    public static void main(String[] args) throws IOException {
        String inputFolderName = "data";
        String outputFolderName = "data_text";
        System.out.println("Extracting text data from '" + inputFolderName + "' to '" + outputFolderName + "'");

        FileUtils.deleteDirectory(new File(outputFolderName)); // Delete the old file in this directory
        System.out.println("Delete all of the old files in directory'" + outputFolderName + "' successfully \n");

        if (new File(outputFolderName).mkdir()) {
            System.out.println("Created folder '"+ outputFolderName );
        }

        File inputFolder = new File(inputFolderName);
        File[] listOfFiles = inputFolder.listFiles();

        String fileName;

        for (File file : listOfFiles) {
            if (file.isFile()) {
                fileName = file.getName();
                System.out.println("\n" + fileName);
                BodyContentHandler handler = new BodyContentHandler();
                AutoDetectParser parser = new AutoDetectParser();
                Metadata metadata = new Metadata();
                ParseContext parseContext = new ParseContext();
                FileInputStream stream = new FileInputStream(new File(inputFolderName + "/" + fileName));
                try {

//////////////////////////////////// Error: Prohibited package name: java.sql ////////////////////////////////
//////////////////////////////////// /////////////////////////////////////////////////////////////////////////

                    parser.parse(stream, handler, metadata, parseContext);

                } catch (Exception e) {
                    System.out.println("Warning: Error when processing file:" + fileName
                            + " . This file will be igrored! \n" + e.getMessage() + "\n" + e.toString());
                    e.printStackTrace();
                    continue;
                } finally {
                    stream.close();
                }
                String s = handler.toString();
                Writer writer = null;
                try {
                    writer = new BufferedWriter(new OutputStreamWriter(
                            new FileOutputStream(outputFolderName + "/" + fileName + ".txt"), "utf-8"));
                    writer.write(s);
                } catch (IOException ex) {
                    // report
                    System.out.println("Warning: Error when saving file:" + fileName
                            + ".txt  . This file had been ignore! \n" + ex.getMessage());
                    continue;
                } finally {
                    try {
                        writer.close();
                    } catch (Exception ex) {
                        /* ignore */}
                }
            }

        }
        System.out.println("Extracting text data from document files has been completed!");
        return;

    }

}

java.lang.SecurityException: Prohibited package name: java.sql
        at java.base/java.lang.ClassLoader.preDefineClass(Unknown Source)
        at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
        at java.base/java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.base/java.net.URLClassLoader.defineClass(Unknown Source)
        at java.base/java.net.URLClassLoader.access$100(Unknown Source)
        at java.base/java.net.URLClassLoader$1.run(Unknown Source)
        at java.base/java.net.URLClassLoader$1.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.net.URLClassLoader.findClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:113)
        at Views.TestTika.main(TestTika.java:43)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

2 个答案:

答案 0 :(得分:7)

当您使用以“java”开头的类的类时,将抛出“禁止的包名称”。在你的rt.jar中找不到。你自己创建了这样一个类,或者你的类路径中有一个包含这样一个类的.jar文件。

如果是前者,请将该课程放在另一个包中。如果是后者,请尝试查找包含此类的.jar文件(例如,打印出系统属性java.class.path中找到的类路径)

答案 1 :(得分:1)

通过Eclipse(Mars)创建Runnable JAR时遇到此问题。

在Jar生成器中,我将库处理选项设置为

  

将所需的库打包到生成的JAR中

但切换到

  

提取所需的图书馆

解决了问题