How to call remote JVM class method - java

My application code will run on one box. I have a tool that will be executed on a different box. Here I want to access my application code specific class method in the tool where it will executed on different box. How can I do this?
I don't want to change any existing code on the application side, I only want to add code on tool side to access the application class method. The class that I want to access is a regular java bean class.

We used to have a somehow similar issue.
We end up creating a simple library that allow us to distribute classes over several JVMs and to call methods in remote JVM.
You can have a look on https://github.com/plantuml/remotejvm to see if it can help you.

May be you need to have a look at Remote Method Invocation. Also take care of which version you are using, versions before Java 5.0 required the RMI stubs to be compiled separately.
Java RMI Tutorial
RMI online training

You would want to take a look at remote method invocation(RMI). It is not possible without adding code on the application side since you need to register the application with the RMI registry. The RMI registry is sort of a directory lookup to allow remote applications to access the application.
You could also use RMI-IIOP but it has the same constraints. You need to register the application with tnameserv
Link: https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/index.html

Related

Running Java jars as windows service

I have an executable jar and I was trying to create a Windows Service using sc.exe. I used the below code for creating service:
sc create "TestService" binPath= "C:\Program Files\Java\jdk1.6.0_03\jre\bin\java.exe -jar C:\abc\MainClass.jar"
The service got created but when I was trying to start the service I got the below error:
Error 1053: The service did not respond to the start or control request in a timely fashion.
Later I tried to use Java Service Wrapper (Community Edition), the service starts for some time but is getting stopped everytime. The wrapper log tells something like:
Advice:
The Wrapper consists of a native component as well as a set of classes
which run within the JVM that it launches. The Java component of the
Wrapper must be initialized promptly after the JVM is launched or the
Wrapper will timeout, as just happened. Most likely the main class
specified in the Wrapper configuration file is not correctly initializing
the Wrapper classes:
com.MainClass
While it is possible to do so manually, the Wrapper ships with helper
classes to make this initialization processes automatic.
Please review the integration section of the Wrapper's documentation
for the various methods which can be employed to launch an application
within the Wrapper
Could anyone please tell me how can I run jar as a Windows Service without using external software as I can't use any third party app on Client's prod env.
If not what other configs I need to do in Java Service Wrapper to make the service start.
I tried to find some info related to this on stackoverflow but I did not get anything thing. If any one has anything on stackoverflow please feel free to put this in comment.
I have used this approach before in a productive environment, so I can assure you it is safe to use.
The Jar-File is wrapped in an exe and then it is added to the windows service scheduler (or however you want to call this). If you have a maven project this is also really easy to accomplish.
https://dzone.com/articles/installing-a-java-application-as-a-windows-service
Edit: you said you can’t use external software. With this approach everything that is needed is packed in the exe file. Including a JRE, I hope that that is allowed by your client’s policy.

How to avoid writing Java code in Eclipse/NetBeans when using Tomcat?

I am currently helping a small hosting company. There is no experience existing in regards to writing Java code.
They now have the order of a customer to host a complicated product using Tomcat, which needs some prelimanary work to be done beforehands. In detail, some Java Proxy classes need to be created using NetBeans (and Eclipse).
I think this is subject to be done by the software manufacturer. However when starting to work with this topic following a documentation of the manufacturer I see that i.e. when creating a WSDL the connect to an internal server (inclusing user name/password) is necessary.
So I wonder how to have this work to be done by the manufacturer without having access to our webserver? I.e. creating a WAR-file?
Usually, the developers should create a deployable artifact that - if the Tomcat itself is configured correctly - simply needs to be deployed and will run out of the box. That is the war file! So basically there is no need to access the hosting company server itself, neither to write any code in Eclipse/NetBeans to get the application up and running. If the customers say so, they either have a really weird code base there, or they simply do not know what they are talking about.

Referencing ClassLoaders in another VM

So I'm creating a profiler application. It has a GUI class which is used to control the profiler. It consists of 2 pieces at runtime:
1) An application (GUI) for control
2) An agent to be loaded into the target application
It lists out PIDs, and then calls loadAgent() on the VM. So I want all of the logic to be done in the primary application. The idea I had was to get an instance of the Instrumentation class from my agent, then invoke a callback method via reflection. This would set a static field in the primary application to the Instrumentation instance, and notify the primary application that the agent is successfully loaded.
The issue I'm having is that I'm unable to grab the primary application's class which contains the Instrumentation field. Class.forName(String) implicitly looks in the ClassLoader for that application. The other method can look for classes in another ClassLoader...unfortunately I have been entirely unable to get the ClassLoader for the agent/target application from the primary application to pass into it!
I see no methods in the Instrumentation or VirtualMachine classes that allow me to do this. I haven't found any questions of this nature on the internet that are answered in a satisfactory method either.
I had some ideas...
A) If there is some kind of equivalent of Class.forName for ClassLoaders maybe I could serialize the ClassLoader and send the serialized string over a socket, then grab it that way. Thus far I haven't found anything remotely like this.
B) Navigate through the ClassLoaders in the agent (there is a ClassLoader.getParent() method..but that's the only means of navigation I see), and find the proper ClassLoader with a deep equals comparison. I don't even know where to begin..I was hoping I could iterate through VirtualMachines.list() and there would be something I could call. There isn't.
C) Load the agent in the primary application, and somehow pass that specific instance of the agent to the target application. Since they are different ClassLoaders I can only assume this doesn't work. For all I know there may be a way to do it though?
D) Create a custom ClassLoader in the primary application and load the target application using that. The issue here is I want full "attach" functionality which this prevents.
I really don't want to put all the logic in the agent, so I thought I would ask you guys before going that route. Googling this has been less than fruitful. Thanks in advance!
I think that you misunderstand how the attach API works in this case. The attach API allows to register an agent from a local VM process to a remote JVM process. After attachment, the remote VM executes the agent's agentmain method in a designated thread. The agent is not aware of its attachment process and cannot communicate objects from the remote heap to the attaching VM's heap.
Also, ClassLoaders are not normally serializable. You would rather need to communicate necessary information via some socket between processes. For this purpose, the local VM can for example communicate a socket address as an argument to the agent on which the local process later waits for information to arrive from the remote VM. In this cause, you should also consider what information you want to be exposed from the remote VM. You probably do not want to send serializable objects.

Reflections on tomcat server from the outside

As part of a challenge, I would like to use reflections to get information about the code running on a tomcat server as *.jsp. (Assuming that the server is not configured very secure and allows this).
Google shows absolutely no answer regarding tomcat and reflections from the outside.
I guess I might have to combine it with Remote Procedure Calls or sth like that. Any ideas?
You don't get free exploits just because Java has reflection.
Reflection (as in java.lang.reflect.*) works only from the inside. Code executing in a JVM process can reason about other code running in the same context, IOW, the program can reflect about itself.
You would need to be able to inject your spying code into the server's java process.
Luckily, servers generally don't allow you to do that, not even badly configured ones - unless there is a remote code execution vulnerability like CVE-2013-4444 for example.
You also can't abuse Remote Procedure Calls that easy. First of all, a remotely callable procedure must be placed there by the programmer. And there are none by default. But assuming you find something that is for some reason unprotected, you'd still only be allowed to call that procedure, not arbitrary code of your choice. If you can, you've probably found a vulnerability.

How can I connect java class with already running daemon

I have created a java application that frequently gathers data from a source and saves it to an sql database. I am also developing another application to operate on the data that is being saved to the database.
My problem is that I will be continuously modifying the operating classes but want to leave the gathering application up all the time so as not to miss any data, but the operating class needs to connect to the gathering class, and I am not sure exactly what the best way to do this is.
If possible I want the two as well connected as possible, just as if one was constructed by the other. What is the best way to do this in java? I am looking into RPC but not sure if it will be as fast as the above scenario.
Any advice would be greatly appreciated!
If you use an OSGi container like karaf you can update jars used in a running program. It has a web interface and command line interface to help you do that.
Another option is to have two programs where one talks to the other. This way you can restart one with a new version of code without stopping the other.

Categories