Java - Executing jar fails because of Prohibited package name: java.sql - java

I am making a multiple choice quiz that makes use of the sqlite-jdbc-3.21.0.jar driver. You can find it here:
https://bitbucket.org/xerial/sqlite-jdbc/downloads/
I've made a class called Database to handle everything relating to SQLite:
package quizpack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Database {
Console cnsl = new Console();
ResultSet rs = null;
Connection connection = null;
Statement stmt = null;
public Connection connect() {
try {
connection = DriverManager.getConnection("jdbc:sqlite:./data/data.db");
Class.forName("org.sqlite.JDBC");
} catch (Exception e) {
cnsl.println("connect() error: " + e.getMessage());
}
return connection;
}
}
When i run my quiz in Eclipse it doesn't give any errors. When i compile it it doesn't give any errors (packaged required libraries). As soon as i launch the jar file in commandprompt i get the message: Prohibited package name: java.sql.
That would be: java.lang.SecurityException: Prohibited package name: java.sql
This is the stacktrace of the connect() error:
connect() error: Prohibited package name: java.sql
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 quiz.Database.connect(Database.java:21)
at quiz.Database.buildQuery(Database.java:51)
at quiz.Quiz.<init>(Quiz.java:22)
at quiz.Main.main(Main.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Nativ
e Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknow
n Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Un
known Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
What i tried:
Changing the package name.
Changing the sequence of my buildpath.
Tried several placings of where i put the driver jar in my source folder.
Signing my exported jar.
Added the jar to my classpathvariables:
database connection not working in jar, but does work in eclipse
Now i'm a beginning Java programmer, i might have done something wrong from the above list. But i'm hoping someone can shed light on what is actually causing this error to happen.

This seems like a repeat of Prohibited package name: java, but you said you changed your package name.
If you didn't change the package name from java to something else, try changing that. Else, delete your jar file and rebuild.

Related

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.sql

I have a jar generated with jdk 1.6, it's working fine with jdk 1.7 and 1.7 but I get the following exception when trying to run it with 1.9 :
Exception in thread "main" 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 java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.base/java.lang.Class.getMethodsRecursive(Unknown Source)
at java.base/java.lang.Class.getMethod0(Unknown Source)
at java.base/java.lang.Class.getMethod(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:57)
my main class :
package eai_principale;
import com.siebel.data.SiebelException;
import java.io.IOException;
import java.sql.SQLException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class MainExecute
{
public static void main(String[] args)
throws SQLException, SiebelException, ParserConfigurationException, SAXException, IOException
{
System.out.println("-----------------START EAI CONNECTOR EXECUTION------------------");
Principale execute = new Principale();
String ligneId = args[0];
String path = args[1];
execute.execute(ligneId, path);
System.out.println("-----------------END EAI CONNECTOR EXECUTION------------------");
}
}
Could you help me ?
I am using Eclipse, and I am using jdbc lib .
Thx
Looks like a bug in Eclipse JDT, specifically in the jarinjarloader part. It is referenced as Bug 525885 - jarinjarloader with jdbc jar does not work in jre9 in Eclipse Bugzilla.
The bugzilla article suggests that using a standard MANIFEST could be a workaround:
The thing is that my jar will execute in jre9 if I manually remove the jarinjarloader portion and use a standard MANIFEST file.

Skype error? Or Eclipse error?

I tried making a simple project for my skype-bot, and it looks like this:
import com.skype.Skype;
import com.skype.SkypeException;
public class SkypeDemo {
public static void main(String[] args) throws SkypeException {
System.out.println(Skype.getVersion());
}
}
But when i started it i get this error:
Exception in thread "main" com.skype.SkypeException: Loading libskype.jnilib failed.
at com.skype.Utils.convertToSkypeException(Unknown Source)
at com.skype.Utils.getProperty(Unknown Source)
at com.skype.Skype.getVersion(Unknown Source)
at SkypeDemo.main(SkypeDemo.java:6)
Caused by: com.skype.connector.LoadLibraryException: Loading libskype.jnilib failed.
at com.skype.connector.ConnectorUtils.loadLibrary(Unknown Source)
at com.skype.connector.osx.SkypeFramework.init(Unknown Source)
at com.skype.connector.osx.OSXConnector.initializeImpl(Unknown Source)
at com.skype.connector.Connector.initialize(Unknown Source)
at com.skype.connector.Connector.connect(Unknown Source)
at com.skype.connector.Connector.assureAttached(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
at com.skype.connector.Connector.execute(Unknown Source)
... 3 more
So how do I fix this error? Keep in mind I have both Skype jars in the build path.
The error is telling you that it cannot load a native library ("libskype.jnilib"). This is (probably) neither Eclipse or Skype's fault.
It is most likely your fault because either the native library is not where it needs to be (or you haven't told Eclipse where to look), or because you have the wrong flavour of native code (DLL, .so or whatever)
(If there was another chained "cause" in the stack trace, it might tell you more ...)
Reference:
This blog post explains how to add a native library to an Eclipse project - http://www.eclipsezone.com/eclipse/forums/t49342.html (In fact, it describes two ways ...)

RMI tutorial errors with ComputeEngine

So I've spent quite awhile trying to find an answer from other people having trouble with the RMI tutorial, but I'm completely stumped on this. I'm doing this tutorial through eclipse.
My ComputeEngine Class. This is just copied from the tutorial, so I don't think there's anything wrong with it.
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;
public class ComputeEngine implements Compute {
public ComputeEngine() {
super();
}
public <T> T executeTask(Task<T> t) {
return t.execute();
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "Compute";
Compute engine = new ComputeEngine();
Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(name, stub);
System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception:");
e.printStackTrace();
}
}
}
I start the rmiregistry in the command line with
set classpath=
start rmiregistry
My VM arguments in eclipse are:
-Djava.rmi.server.codebase=file:/C:/Users/Kevin/workspace/RMI/bin/
-Djava.rmi.server.hostname=Compute
-Djava.security.policy=server.policy
I have the compute.jar file and the the server.policy files in the bin folder. I granted all permissions for the policy file.
grant{
permission java.security.AllPermission;
};
After all that, I run ComputeEngine and get the following errors:
ComputeEngine exception:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at engine.ComputeEngine.main(ComputeEngine.java:31)
It seems like it has some kind of problem with rebind, but I don't understand what. I also don't understand the AccessControlException, when I have the policy file. I've checked to make sure the rmiregistry is still running, and I don't close the empty window that comes up after starting it.
So yeah, I'm lost.
Clearly your security policy file isn't being found. It would need to be in the current working directory when you execute the program. Run your program with -Djava.security.debug=access,failure to see exactly what's going on.
The exception clearly says that your code base does not have permission to creat sockets/ for network communication. The reason could be with your security policy specification alone. Do not specify the policy file explicitly and allow the JVM to use the default security policy. The default policy specifies the right permissions so you should be fine.

MySQL jdbc driver and Eclipse problems

I am having problems loading the MySQL JDBC driver. I have tried everything mentioned here, but am still running into problems.
The error I get is this:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Main.java:12)
My code is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
public static void main(String args[]) throws Exception {
//accessing the driver
Class.forName("com.mysql.jdbc.driver");
// creating variable to pass the connection information into
Connection dbcon = DriverManager.getConnection(
"jdbc:mysql//localhost:3306/test", "root", "root");
PreparedStatement statement = dbcon
.prepareStatement("select * from names");
ResultSet result = statement.executeQuery();
while (result.next()) {
System.out.println(result.getString(2));
}
}
}
As you can see above I have placed the driver in the lib folder and I have also put it into the Apache tomcat lib folder, because that was suggested as well.
Thank you for any help you can give me.
Andrew
The class name inside the MySql connectorJ JAR file is Driver. so you need to change
Class.forName("com.mysql.jdbc.driver");
to
Class.forName("com.mysql.jdbc.Driver");
It tells you the problem on the first line of your stacktrace:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
The driver name is com.mysql.jdbc.Driver (capital D).
problem in loading the mysql connection library.please check the proper mysql library is load successfully.because when library load successfully then the library package display all the external loaded library.

java connectivity with mysql error

I just started with the connectivity and tried this example. I have installed the necessary softwares. Also copied the jar file into the /ext folder.Yet the code below has the following error
import java.sql.*;
public class Jdbc00 {
public static void main(String args[]){
try {
Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url =
"jdbc:mysql://localhost:3306/mysql"
DriverManager.getConnection(url,"root", "root");
//Display URL and connection information
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
//Get a Statement object
stmt = con.createStatement();
//Create the new database
stmt.executeUpdate(
"CREATE DATABASE JunkDB");
stmt.executeUpdate(
"GRANT SELECT,INSERT,UPDATE,DELETE," +
"CREATE,DROP " +
"ON JunkDB.* TO 'auser'#'localhost' " +
"IDENTIFIED BY 'drowssap';");
con.close();
}catch( Exception e ) {
e.printStackTrace();
}//end catch
}//end main
}//end class Jdbc00
But it gave the following error
D:\Java12\Explore>java Jdbc00
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Jdbc00.main(Jdbc00.java:11)
Could anyone please guide me in correcting this?
The jar file that contains the MySQL driver class (com.mysql.jdbc.Driver) isn't being found on the classpath when you run your application. This is what the ClassNotFoundException is complaining about.
You'll need to add it either to the CLASSPATH environment variable, or using the classpath option when running Java. For example:
java -cp mysql-connector-java-5.0.8-bin.jar Jdbc00
Use the name and location of whatever MySQL connector jar file you're using. (If you haven't already installed MySQL on localhost, so your application has something to connect to, you might have to do that too.)
As #Ash says, the problem is that the Connector/J drivers are not on your classpath. You can download the latest version (5.0.12) from this page.

Categories

Resources