Unable to start derby database from Netbeans 7.4

╄→尐↘猪︶ㄣ 提交于 2019-11-27 17:29:57
user2060065

This is what I did:

  1. Find out exactly where the java home is by executing this instruction from NetBeans 7.4 :

    System.out.println(System.getProperty("java.home"));

    This is the output for my case:

    C:\Program Files\Java\jdk1.7.0_51\jre

    which is quite important for me, I was modifying another java.policy and took no effect and wasted me a couple of hours.

  2. For reason of java.policy is an unix style file and read-only, I opened and edited it with notepad++ and executed as administrator (under the same java home):

    C:\Program Files\Java\jdk1.7.0_51\jre\lib\security\java.policy

    Add only these lines into the file after the first grant:

    grant {
        permission java.net.SocketPermission "localhost:1527", "listen";
    };
  3. Save the file, which is a little tricky for reason of the permission. But if you run notepad++ or any other edit program as administrator, you can solve the problem.

    Then try to connect the database from NetBeans, it works for me.

Good luck.

According to Java™ SE Development Kit 7, Update 51 Release Notes

Change in Default Socket Permissions

The default socket permissions assigned to all code including untrusted code have been changed in this release. Previously, all code was able to bind any socket type to any port number greater than or equal to 1024. It is still possible to bind sockets to the ephemeral port range on each system. The exact range of ephemeral ports varies from one operating system to another, but it is typically in the high range (such as from 49152 to 65535). The new restriction is that binding sockets outside of the ephemeral range now requires an explicit permission in the system security policy.

Most applications using client tcp sockets and a security manager will not see any problem, as these typically bind to ephemeral ports anyway. Applications using datagram sockets or server tcp sockets (and a security manager) may encounter security exceptions where none were seen before. If this occurs, users should review whether the port number being requested is expected, and if this is the case, a socket permission grant can be added to the local security policy, to resolve the issue.

This means that you have to explicity set the permissions for your application to be able to access the ports range between 1025 and 49151. You can therefore grant this permission by appending this line in the list of permissions granted:

Visit your Java Home Directory and access your policy file at $JAVA_HOME/jre/lib/security/java.policy and make the following changes.

grant{
     //List of granted permissions
     permission java.net.SocketPermission "localhost:1527", "listen";
}
Chuk Lee

See http://www.oracle.com/technetwork/java/javase/7u51-relnotes-2085002.html for the description of the "problem". Search other-libs/javadb

Depending on your requirement, what I did was go and modify the default security policy

cd $JAVA_HOME/jre/lib/security

Edit java.policy (make a backup first!)

Add the following

grant codeBase "file:${java.home}}/../db/lib/*" {
        permission java.security.AllPermission;
};

Note that this is my requirement.

I'm granting every app who uses the u51 JRE the permission to start Derby.

EDIT

The alternative would be to use a less permissive set of permissions like:

grant codeBase "file:${java.home}}/../db/lib/*" {
    permission java.net.SocketPermission "localhost:1527", "listen,resolve";
};

NetBeans, by default, uses the derby version installed with GlassFish. So my permissions look like this on the Mac. It will be similar on Windows, but the path will need to change.

grant codeBase "file:/Applications/NetBeans/glassfish-4.0/javadb/lib/*" {
    permission java.net.SocketPermission "localhost:1527", "listen,resolve";
};

Because the upper measures didn't work I added the following permission to the end of the main permission section:

permission java.net.SocketPermission "localhost:1527", "listen,resolve";

You can also solve the problem on a per-user basis by granting the needed permission in a file called .java.policy in your home directory.

Works on both Unix and Windows systems as documented here: http://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html

This might be useful if the system-wide policy file gets overwritten, for example when updating your JDK, or if you don't have permission to edit the system file.

This is what I have in my $HOME/.java.policy:

grant {
    permission java.net.SocketPermission "localhost:1527", "listen";
};

I got a bit fed up with Oracle's approach to security lately. They seem to be trying to protect us from ourselves in ways that would be more appropriate to naive users than programmers. My view is that the code I put on my own machine should be able to do whatever it needs to. It's my fault if I put code there that does bad things. Clearly not a universally reliable perspective, but it's worked for me for about 35 years. On that basis, I add this to my /lib/security/java.policy file:

grant codeBase "file:/-" {
    permission java.security.AllPermission;
};

note that the file:/- matches any file on the system, and the grant block says, in essence, "if the class is loaded from this file system, then trust it".

This was doing my head in for a bit until I stumbled across the following in the NetBeans wiki

JavaDB grant permissions

JavaDB grant permissions

How to grant permissions for Java DB / How to start Java DB

Related to issue #239962

JDK 7u51 comes with some security improvements which are causing problems with starting Java DB on this Java version.

When you try to start DB from NetBeans you will probably get the Exception:

java.security.AccessControlException: access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")

The same exception you will get while starting using script /db/bin/startNetworkServer

Because there is no suitable way to fix it on the NetBeans side and this should be fixed on the side of the Java DB.

There are several ways how to deal with this problem. I will mention only the easiest way. You have to start DB manually from command line.

• Start Java DB with -noSecurityManager argument.

(JDK 7u51 location)/db/bin/startNetworkServer -noSecurityManager

Although it’s not exactly a solution it is usable as a quick workaround.

My solution to this was to reinstall jdk 1.7.45, uninstall netbeans and reinstall it selecting the outdated jdk. Don't know if there is a way to change sdk in NB without reinstalling it but it worked this way.

Well, one alternative is to change the port JavaDB listens to, to be now in the high range (such as from 49152 to 65535). Go to Window->Services, then right click Java DB and in "Java DB Properties Dialog" goto to "Database Location", which in my system is "C:\Users\ahernandeza.netbeans-derby" In that directory edit or create the file derby.properties, and add/edit the line: derby.drda.portNumber=XXXX Where XXXX is the new port, in my case i put 51527 and worked just fine.

EDIT At fisrt glance it worked, the service started just fine, but when creating or starting a database in NB, i got the error Unable to connect. CAnnot establish a connection to jdbc:derby://localhost:1527/sample Although i changed the pprt to 51527, it tries to connect to 1527

If linux, then

file=`find $(dirname $(readlink -f $(which java)))/.. -iname 'java.policy'`; grep 1527 $file || sudo sed -i '0,/"listen"/{s/"listen".*/\0\n\tpermission java.net.SocketPermission "localhost:1527", "listen";/}' $file
cat $file

it automatically finds your java and changes permissions

I found a quick solution to this problem - Start your JavaDB from the command line\terminal like so:

<base folder>/db/bin/startNetworkServer -noSecurityManager

Then it runs fine without adding new permissions.

The problem is the Java 7u51, it have a bug that affect Derby and other programs and libraries, I suggest to install the Java 7u45

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!