RMI tutorial errors with ComputeEngine - java

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.

Related

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

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.

Microsoft Translator API error retrieving translation

I'm simply trying to run this sample code below:
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class Translator {
public static void main(String[] args) throws Exception {
Translate.setClientId("ID GOES HERE");
Translate.setClientSecret("SECRET GOES HERE");
String translatedText = Translate.execute("Bonjour le monde",
Language.FRENCH, Language.ENGLISH);
System.out.println(translatedText);
}
}
and I'm getting the following Exception:
Exception in thread "main" java.lang.Exception: [microsoft-translator-api] Error retrieving translation : datamarket.accesscontrol.windows.net
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
at com.memetix.mst.translate.Translate.execute(Translate.java:61)
at Translator.main(Translator.java:10)
Caused by: java.net.UnknownHostException: datamarket.accesscontrol.windows.net
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
... 2 more
I know it seems like I'm not even trying to figure this out on my own but I'm a complete beginner and can't really understand the Exception trace at all by myself. I'm pretty sure I got the right client Secret. In my azure account I only see an application ID and an Object ID. I'm using the application ID as the client ID.
Does anyone have any ideas on what might be causing this? Any help is greatly appreciated.
Thank you!
The third party Java wrapper boatmeme/microsoft-translator-java-api for MS Azure Translator API is too old & unavailable, because it wrappered the old Microsoft Translator - Text Translation which is old & unavailable now.
There is a notice at the page top of the site Azure datamarket.
DataMarket and Data Services are being retired and will stop accepting new orders after 12/31/2016. Existing subscriptions will be retired and cancelled starting 3/31/2017. Please reach out to your service provider for options if you want to continue service.
For using the new Azure Translator API on Azure portal, you need to refer to the document Announcements: Microsoft Translator Moves to the Azure portal to know how to create the new one on Azure portal and use it via the new REST APIs. Meanwhile, just as reference, you can see my answer in Java for the other SO thread Microsoft Translator API Java, How to get client new ID with Azure.
Hope it helps.

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 ...)

Java Applet SOAPMessage causes "SAAJ0540: Error during saving a multipart message"

I have a problem which I have boiled down to this simple example.
import java.applet.Applet;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
public class SOAPTest2 extends Applet {
private static final long serialVersionUID = -1;
public void init(){
try{
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
System.out.println("About to fail");
sm.writeTo(System.out);
System.out.println();
System.out.println("If you see this, it didn't fail.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I run this in eclipse (JDK 1.6.0_27) It works as I would expect, and I get the following output:
About to fail
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body/></SOAP-ENV:Envelope>
If you see this, it didn't fail.
When I run this in a browser as an applet I get the following error in the java console.
About to fail
14-Sep-2011 1:20:51 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl saveChanges
SEVERE: SAAJ0540: Error during saving a multipart message
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Error during saving a multipart message
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.saveChanges(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.writeTo(Unknown Source)
at SOAPTest2.init(SOAPTest2.java:16)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.xml.internal.messaging.saaj.util.FastInfosetReflection.<clinit>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.util.transform.EfficientStreamingTransformer.transform(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl.output(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl.output(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getContentAsStream(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getHeaderBytes(Unknown Source)
... 5 more
Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission com.sun.xml.internal.fastinfoset.parser.string-interning read)
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.checkPropertyAccess(Unknown Source)
at java.lang.System.getProperty(Unknown Source)
at com.sun.xml.internal.fastinfoset.Decoder.<clinit>(Unknown Source)
... 13 more
I am at a loss as to why this is occurring. I am simply creating en empty SOAPMessage and trying to write it to System.out. I am not accessing any local resources. I have done much searching on MessageImpl.saveChanges and FastInfosetReflection to no avail. Please give the example a try, and let me know if you have any ideas.
Respectfully, I ask that any answers that tell me to sign my Jar be accompanied by a reasonable explanation given the minimal example above that does not access local resources.
The exception is self explanatory:
Caused by: java.security.AccessControlException: access denied
(java.util.PropertyPermission
com.sun.xml.internal.fastinfoset.parser.string-interning read)
SAAJ imposes security restrictions and you have no problem when you run in Eclipse but when running from Applet, you run inside a sandbox and security manager stops the flow
Definitely a signing issue.
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/signed.html
You can try it out with the exact values from that link (in this sample the only thing that is different is that the jar is called sample.jar):
Build a jar called sample.jar
Run: keytool -genkey -alias signFiles -keystore compstore -keypass kpi135 -dname "cn=jones" -storepass ab987c
Run: jarsigner -keystore compstore -storepass ab987c -keypass kpi135 -signedjar Ssample.jar sample.jar signFiles
I should add that I know this is the solution since I work with FuryComputers ... I have no idea why it is the solution though - anyone else have any ideas?

Java file input and output

I have the below method which is meant to append information to a file but I get the error below. In the method I use parts of robocode API which inherits from java.io.InputStream
All the permissions on the files and folders seem fine and the file does exist
static public void logInfo(String info)
{
RobocodeFileWriter in;
try {
in = new RobocodeFileWriter("log.txt");
in.append(info);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
SYSTEM: An error occurred during initialization of itc.solomon
SYSTEM: java.security.AccessControlException: Preventing itc.solomon from access: (java.io.FilePermission log.txt read): You may only read files in your own root package directory.
java.security.AccessControlException: Preventing itc.solomon from access: (java.io.FilePermission log.txt read): You may only read files in your own root package directory.
at robocode.security.RobocodeSecurityManager.handleSecurityProblem(Unknown Source)
at robocode.security.RobocodeSecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
at java.io.File.exists(File.java:748)
at robocode.RobocodeFileOutputStream.(Unknown Source)
at robocode.RobocodeFileOutputStream.(Unknown Source)
at robocode.RobocodeFileWriter.(Unknown Source)
at itc.CFile.logInfo(CFile.java:16)
at itc.solomon.(solomon.java:43)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at robocode.peer.proxies.HostingRobotProxy.loadRobotRound(Unknown Source)
at robocode.peer.proxies.HostingRobotProxy.run(Unknown Source)
at java.lang.Thread.run(Thread.java:636)
See this sample:
http://code.google.com/p/robocode/source/browse/robocode/trunk/robocode.samples/src/main/java/sample/SittingDuck.java
I think getDataFile("log.txt") will give you proper location.
I've searched a bit about this problem. It seems that there's a bug in the robocode package, awkwardly solved by:
while (!dataIsLoaded) {
try {
tryToReadData;
dataIsLoaded = true;
} catch (AnyException e) {}
}
It's a quite awful way to solve the problems, for many reasons (ignoring exceptions, busy-waiting, etc.) A more sane way would be downgrading the robocode package to a previous, more stable version.
See search results here.

Categories

Resources