java.rmi.NoSuchObjectException - java

I have had a look here, here and here however I have absolutely no idea why I am coming across this error.
I am of the opinion that I have strong references (code at end of post), I am not explicitly calling GC (not that that particular method does much) and yet I just cannot boot this damn RMI server.
It should be noted I am extremely new to RMI. I have read the Oracle trail, that didn't work. Tried another tutorial, same problem. In fact, no matter how I approach this, I cannot get past this ObjectNotFoundException.
Now I know that this means the object no longer exists, but why doesn't it exist... I have a static reference to it in the main class, the VM is seriously misbehaving if it is doing this.
Running Arch Linux, oracle's VM and Eclipse kepler.
package engine;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import service.Service;
public class Main {
private static Service stub;
private static Service engine;
public static void main(String[] args) {
System.setProperty("java.security.policy", "file:///home/jameshey/git/MicroSense/release/conf/rmisecurity.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "HelloWorldService";
engine = new ServiceEngine();
Registry registry = LocateRegistry.getRegistry(1099);
stub = (Service) UnicastRemoteObject.exportObject(engine, 1099);
registry.bind(name, stub);
System.out.println("ServiceEngine bound");
} catch (Exception e) {
System.err.println("ServiceEngine exception:");
e.printStackTrace();
}
}
}
I have tried pretty much every forum solution I can find. I have tried no statics and combinations of which variables are static. Completely stumped.
The Stack Trace of the Exception is:
ServiceEngine exception:
java.rmi.NoSuchObjectException: no such object in table
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at engine.Main.main(Main.java:25)

LocateRegistry.getRegistry() doesn't create a Registry. It just creates a stub that may or may not actually work, depending on whether the Registry is running. Change it to createRegistry(). Make the Registry variable static in the server.

Related

How to implement a SocketImplFactory using systems default sockets

I have a small problem with implementing a own SocketImplFactory in Java.
My goal is to write a factory which offers me a way to close all open sockets with one simple method call. So I only want to have a kind of "proxy factory" which stores all the created sockets in a list. On this list I could perform all the actions I need.
I tried to implement it like this:
package java.net;
import java.io.IOException;
import java.net.SocketImpl;
import java.net.SocketImplFactory;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import com.crosscloud.applicationlayer.logger.CCLogger;
public class CCSocketImplFactory implements SocketImplFactory
{
private List<SocketImpl> _openSockets;
public CCSocketImplFactory()
{
_openSockets = new LinkedList<>();
}
#Override
public SocketImpl createSocketImpl()
{
SocketImpl impl = new SocksSocketImpl();
_openSockets.add(impl);
return impl;
}
public void closeAll()
{
_openSockets.forEach((socket)->
{
try
{
socket.close();
}
catch (Exception e)
{
logException(this, e);
}
});
}
public static CCSocketImplFactory register()
{
CCSocketImplFactory fact = new CCSocketImplFactory();
try
{
Socket.setSocketImplFactory(fact);
}
catch (IOException e)
{
logException(CCSocketImplFactory.class, e);
}
return fact;
}
The problem I have now is that I have to create the class in the package java.net because the class SocksSocketImpl(In my opinion this should be the standard type) is only visible in this package.
When I now want to run the code I get a SecurityException because the package name is probhibited.
Is there a workaround for my problem?
Thank you!
It appears that you are trying to use only one class from java.net There is no need to move you class tot hat package just to create an instance of it. I suggest using reflection instead.
Constructor cons = Class.forName("java.net.SocksSocketImpl").getDeclaredConstructor();
cons.setAccessible(true);
SocketImpl si = (SocketImpl) cons.newInstance();
However using SOCKS by default is likely to be a bad idea as it will change the default not just for your sockets, but all sockets even ones for internal use such as JMX or VisualVM.
What would be an alternative instead of always using SocksSocketImpl?
I also found this example which shows some extended possibilities of this method.
Finding out what network sockets are open in the current Java VM

java.lang.NoClassDefFoundError

I have no idea of why this is having a runtime error, I have googled the problem and it says that a class that was available during compile time is no longer available at run time.
This is the code:
package examples.RMIShape;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
//import java.rmi.server.UnicastRemoteObject;
public class ShapeListServer {
public static void main(String args[]){
// System.setSecurityManager(new RMISecurityManager());
System.out.println("Main OK");
try{
ShapeList aShapelist = new ShapeListServant();
System.out.println("After create");
String registryURL = "rmi://localhost:" + "/ShapeList";
startRegistry();
Naming.rebind(registryURL, aShapelist);
System.out.println("ShapeList server ready");
}catch(Exception e) {
System.out.println("ShapeList server main " + e.getMessage());
}
}
// This method starts a RMI registry on the local host, if it
// does not already exists at the specified port number.
private static void startRegistry()throws RemoteException{
Registry registry;
try {
registry = LocateRegistry.getRegistry();
registry.list( ); // This call will throw an exception
// if the registry does not already exist
}
catch (RemoteException e) {
// No valid registry at that port.
System.out.println ("RMI registry cannot be located at port " + Registry.REGISTRY_PORT );
registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
System.out.println("RMI registry created at port " + Registry.REGISTRY_PORT);
}
} // end startRegistry
}
I have googled the problem and it says that a class that was available during compile time is no longer available at run time.
That's not correct. That would cause ClassNotFoundException. This one has several causes, but the most common one is that the class in the file isn the class implied by the filename and directory hierarchy.
Basicall java.lang.NoClassDefFoundError thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.
following links will be helpful. This links will guide you slove issue.
http://javarevisited.blogspot.in/2011/06/noclassdeffounderror-exception-in.html
http://javaeesupportpatterns.blogspot.in/2012/06/javalangnoclassdeffounderror-how-to.html
Hope this will be helpful

How to connect to an mBeanServer in another local process?

If you set the "com.sun.management.jmxremote" system property when you start a JVM, you can run jconsole or visualvm and connect to that local mBeanServer. I want to do the same thing they are doing but can't figure out how.
Is there a service URL you can use to identify a locally running JVM?
I know I could do this by setting up a jmxmp or rmi listener on a specific port and then connecting to that port, but I don't want to do that because it means I have to manage the ports and know which port goes to which jvm (we run multiple jvms on the same server).
Thanks to Nicholas and Scott for the post and discussion. Was able to piece this together based on that info and some trial and error.
Sharing so others don't have to do the same leg work. This will attach to a JVM and get its memory usage.
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import com.sun.tools.attach.spi.AttachProvider;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class AttachFun {
public static void main(String[] args) throws Exception {
final AttachProvider attachProvider = AttachProvider.providers().get(0);
VirtualMachineDescriptor descriptor = null;
for (VirtualMachineDescriptor virtualMachineDescriptor : attachProvider.listVirtualMachines()) {
if (pickThisOne(virtualMachineDescriptor)) {
descriptor = virtualMachineDescriptor;
break;
}
}
if (descriptor == null) throw new RuntimeException("You didn't pick one");
final VirtualMachine virtualMachine = attachProvider.attachVirtualMachine(descriptor);
virtualMachine.loadAgent("/System/Library/Frameworks/JavaVM.framework/Home/lib/management-agent.jar", "com.sun.management.jmxremote");
final Object portObject = virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
final JMXServiceURL target = new JMXServiceURL(portObject + "");
final JMXConnector connector = JMXConnectorFactory.connect(target);
final MBeanServerConnection remote = connector.getMBeanServerConnection();
final ObjectName memory = new ObjectName("java.lang:type=Memory");
CompositeData cd = (CompositeData) remote.getAttribute(memory, "HeapMemoryUsage");
final Long used = (Long) cd.get("used");
System.out.println(used);
}
private static boolean pickThisOne(VirtualMachineDescriptor virtualMachineDescriptor) {
// TODO
return false;
}
}
You need the Attach API. It will locate running [Attach API capable] JVMs on the same host and return meta-data on each as well as enable a JMXConnector. It's basically what you see when you start JConsole and the connect menu displays running JVMs, including Java 1.6+ instances that were not started with any special JMX command line directives.
//Nicholas

Why is inet:/0.0.0.0 interpreted as inet:/0:0:0:0:0:0:0:0 by com.sun.net.httpserver.HttpServer?

We found this issue while implementing a reconnect logic for a WS endpoint using JAX-WS stack deployed on Glassfishv2.1 server. We deploy the web service on a cluster environment. To simplify the deployment, we use 0.0.0.0 as the IP on which the endpoint needs to be published, so that it can be accessed from all available IPs pertaining to the nodes of the cluster. Following is the code snippet for initialization of the WS(Web Service):
import javax.xml.ws.Endpoint;
.
.
//Implementor is the corresponding implementation object for the WS
Endpoint receiver = Endpoint.create(new Implementor());
.
receiver.setExecutor(threadPoolExecutor);
receiver.publish ("http://0.0.0.0:9545/context");
We call receiver.stop() to stop publishing the endpoint in our cleanup code. That's where we receive a null pointer exception with the following stack trace:
java.lang.NullPointerException
at com.sun.xml.ws.transport.http.server.ServerMgr.removeContext(ServerMgr.java:123)
at com.sun.xml.ws.transport.http.server.HttpEndpoint.stop(HttpEndpoint.java:110)
at com.sun.xml.ws.transport.http.server.EndpointImpl.stop(EndpointImpl.java:167
While trying to find the cause of the NPE, we found that the ServerMgr class depends on the InetSocketAddress of the HttpServer that listen on the ip:port of the URL where the WS endpoint is published, to retrieve some state information from a map. Since the inet address "inet:/0.0.0.0" is interpreted as "inet:/0:0:0:0:0:0:0:0" it could not find the entry in the map and hence the NPE. Here is the source code of ServerMgr.
In order to prove that this is in fact the problem, we tried to replicate the logic of the ServerMgr code related to InetSocketAddress as the following program:
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
static final String URL_1 = "http://0.0.0.0:9545/context";
static final String URL_2 = "http://127.0.0.1:9548/context";
static final String URL_3 = "http://10.226.90.217:9549/context";
public void testUrl(String address){
try {
URL url = new URL(address);
Map<InetSocketAddress, Integer> map = new HashMap<InetSocketAddress, Integer>();
InetSocketAddress iaddr = new InetSocketAddress(url.getHost(), url.getPort());
map.put(iaddr, 1);
HttpServer server = HttpServer.create(iaddr, 5);
HttpContext context = server.createContext(url.toURI().getPath());
server.start();
System.out.println("original inet:"+iaddr+" and final inet:"+context.getServer().getAddress());
if(iaddr.equals(context.getServer().getAddress())){
System.out.println("equal");
Integer t = map.get(context.getServer().getAddress());
if( t == null){
System.out.println("You won");
}else{
System.out.println("You lose "+t);
}
}else{
System.out.println("not-equal");
}
server.stop(0);
map.clear();
} catch (URISyntaxException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args)
{
Main d = new Main();
d.testUrl(Main.URL_1);
d.testUrl(Main.URL_2);
d.testUrl(Main.URL_3);
}
}
Odd enough we obtain the following result in my WindowsXP box ( Java version 1.6.0_22)
equal--
original inet:/0.0.0.0:9545 and final inet:/0.0.0.0:9545
equal
You lose 1
equal--
original inet:/127.0.0.1:9548 and final inet:/127.0.0.1:9548
equal
You lose 1
equal--
original inet:/10.226.92.47:9549 and final inet:/10.226.92.47:9549
equal
You lose 1
and the following output on my dev box
(Linux tahoe 2.6.9-67.EL #1 Wed Nov 7 13:43:31 EST 2007 x86_64 x86_64 x86_64 GNU/Linux)
(Java version 1.6.0_17)
run:
original inet:/0.0.0.0:9545 and final inet:/0:0:0:0:0:0:0:0:9545
not-equal
original inet:/127.0.0.1:9548 and final inet:/127.0.0.1:9548
equal
You lose 1
original inet:/10.226.90.217:9549 and final inet:/10.226.90.217:9549
equal
You lose 1
Based on the background - I have two questions:
a. Why is the 0.0.0.0 is interpreted
as IPv6 address ? (In addition, is it a problem
with the OS or the JRE ? Is it a bug
or a feature ? etc)
b. Do we have a way to configure
JRE to interpret 0.0.0.0 as IPv4 address ?
(We want to keep using 0.0.0.0 as the endpoint
address as it simplifies deployment
of our Web Service)
You can replace the 0.0.0.0 with InetAddress.getLocalHost().getHostAddress();
It will automatically resolve correctly and there is no need of -Djava.net.preferIPv4Stack=true
Have you tried by forcing the IPv4 choice as a variable to the java executable?
-Djava.net.preferIPv4Stack=true
Take a look here and here..
It's also possible to force IPv4 from your code with System.setProperty("java.net.preferIPv4Stack" , "true");

Determine if Java App is being run over an RDP Session?

How can I detect if my Swing App is being run from a windows RDP session?
Java only solution preferred, but the app is guaranteed to be running on windows so I'm ok with shelling out.
I think you'll have to invoke the native Windows libraries to pull this off. Try something like this:
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.*;
import com.sun.jna.examples.win32.Kernel32;
...
public static boolean isLocalSession() {
Kernel32 kernel32;
IntByReference pSessionId;
int consoleSessionId;
Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
pSessionId = new IntByReference();
if (lib.ProcessIdToSessionId(lib.GetCurrentProcessId(), pSessionId)) {
consoleSessionId = lib.WTSGetActiveConsoleSessionId();
return (consoleSessionId != 0xFFFFFFFF && consoleSessionId == pSessionId.getValue());
} else return false;
}
That strange-looking condition for consoleSessionId is from the documentation for WTSGetActiveConsoleSessionId, which says:
Return Value
The session identifier of the session that is attached to the physical console. If there is no session attached to the physical console, (for example, if the physical console session is in the process of being attached or detached), this function returns 0xFFFFFFFF.
The above answers might work, but seem needlessly complicated. You can simply read the windows 'sessionname' environment variable to detect RDP sessions. The value of this environment variable will be 'Console' for a normal, local session. For an RDP session it will contain the phrase 'RDP'. It's easy enough just to check for that.
public static boolean isRemoteDesktopSession() {
System.getenv("sessionname").contains("RDP");
}
Tested and confirmed working under Windows7 64bit. One issue I have noticed with this technique is that it appears that environment variable values as read from System.getenv() do not change once the JVM has started. So if the JVM process was started by a console session, but then accessed by an RDP session, further calls to System.getenv("sessionname") still return 'Console.'
Try with NativeCall ( http://johannburkard.de/software/nativecall/ )
All you need is 2 jars plus 1 DLL in your classpath.
A quick test :
import java.io.IOException;
import com.eaio.nativecall.*;
public class WindowsUtils {
public static final int SM_REMOTESESSION = 4096; // remote session
public static boolean isRemote() throws SecurityException, UnsatisfiedLinkError,
UnsupportedOperationException, IOException
{
NativeCall.init();
IntCall ic = null;
ic = new IntCall("user32", "GetSystemMetrics");
int rc = ic.executeCall(new Integer(SM_REMOTESESSION));
if (ic != null) ic.destroy();
return (rc > 0);
}
public static void main(String ... args) throws Exception {
System.out.println(WindowsUtils.isRemote());
}
}

Categories

Resources