"java.lang.SecurityException: Prohibited package name: java.sql" error happen only when executing outside of Eclipse
Asked Answered
O

2

3

I am writing a Topic Modeling program using Apache Tika to extract the text contents from other file type. Actually It run perfectly on Eclipse. But when I export to JAR file to use from command prompt of the Window 10. This error message appear when it try the code: "parser.parse(stream, handler, metadata, parseContext);"

"java.lang.SecurityException: Prohibited package name: java.sql"

I didn't upload my java code here because I don't think they are the root of the problem. Since it run perfectly inside Eclipse IDE. So do anyone know why it only happen when I try to run it from command line. What are the different in JVM between inside and outside of Eclipse IDE? Thank you.

        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)
Ostracod answered 14/3, 2018 at 20:23 Comment(1)
I just added the java code and exception detail in my question. Please note that it still work in Eclipse but the exception will be thrown when I execute it in command line: java -jar TestTika.jarOstracod
S
9

The 'prohibited package name' is thrown, when you are using a class of a package starting with 'java.' that is not found in your rt.jar. Either you created such a class yourself, or you have a .jar file containing such a class in your classpath.

If it's the former, put the class in another package. If it's the latter, try to find the .jar file containing this class (e.g. print out the classpath found in the system property java.class.path)

Shaggy answered 14/3, 2018 at 20:51 Comment(2)
Thank you for your response. But I did not create any package or class start with "java." And I use Apache Tika apache.org/dyn/closer.cgi/tika/tika-app-1.17.jar at tika.apache.org/download.html They've developed it for almost 10yrs I don't think they make that simple error. and I could not find any java package inside tika-app-1.17.jar . I double-checked it by creating a java package just for testing purpose. It throw this exception immediately when I try to run in Eclipse, doesn't need to wait until I export into JAR file and run in command line.Ostracod
You are using some eclipse magic to create the jar file (jarinjarloader), so it'spossible that some wrong class files have been packaged. That's why I recommended to check the classpath. You can also try the -verbose:class option, which will show when a class is loaded. But I don't know when this option prints the class name. So this might not work either.Shaggy
M
4

Ran into this issue when creating a Runnable JAR via Eclipse (Mars).

In the Jar generator I was setting the library handling option to

Package required libraries into generated JAR

but switching to

Extract required libaries

solved the issue

Marjoram answered 15/6, 2018 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.