Running Java jars as windows service - java

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.

Related

Tomcat - Running external JAR/WAR

So I have a little Tomcat web app I am running, with the following structure :
Main Servlet
Class for Job A
Class for Job B
Class for Job C
Tons of classes used for the various jobs executions
The webapp is compiled & deployed via ant. At the moment, I get a nice Project.war with all classes.
Basically, the main servlet works as a dispatcher. Depending of the webrequest received, it launches either Job A, B or C in a new thread.
I want to improve my application so that I can redeploy the class for Job A/B/C without affecting running processes. Here's how I am conceptualizing it :
I deploy the main servlet, used as a dispatcher.
I deploy A.war, B.war, C.war
I run B.war
I redeploy A.war, B.war is still running
I run A.war
B.war is done, its output is sent back via the main servlet.
To be brutally honest, I have no idea on where to start or where to look at. I thought about using a ProcessBuilder and executing the jar/war in command line, but it feels like the most unsafe thing to do.
Any input is appreciated.
Thank you !
I believe you need a modular application where you have multiple plugins (Jobs in your case) which can be loaded or unloaded when you want, and to be used once available. Check this stackoverflow question it can give you a little help.
But for simplicity, in a previous job we needed something acts like OSGI without using OSGI, we built API using serviceLoader and created an interface for our module (in your case job) each jar will have a class which will implement the interface. And once the job is needed you can call for it from the service loader if it exist you can use it, if not throw some error.

How to embed a Wildfly server within an application

I'm building an OSGi application containing (at the moment) only business logic. Since I want my users to interact with my software via browser I'm thinking on trying to add an application server inside my application via commands like:
public void startApplicationServer();
public void stopApplciationServer();
public void deployApp(App appToDeploy);
public void undeployApp(App appToUndeploy);
After some thought, the application server I've chosen is "wildfly 10". However, I'm failing to find any resource allowing me to call wildfly programmatically. My question is do you know a procedure to follow or general steps in order to achieve my goal?
Some info you can find useful to help me:
I've chosen Wildfly becuase it fully support Java EE 7, run on JDK8 and it is released under LGPL (I preferred open source glassfish, but it was release under GPL, hence it would have been mandatory to make the source available. Since (in the future) I would like to make this software commercial, I was forced to discard it);
Maybe some of you may suggest to build my whole application on the application server itself. I prefer not to do so because the web interface may be only one possible User Interface of my application (who knows, maybe in the future I want to switch to another interface, like CLI or desktop-like);
I've look at several content, like wildfly-swarm or Arquillian: I know these projects targets are completely different from mine, but maybe they can be part of the solution? It's just my personal (and possible wrong) thought;
I'm a newbie in the whole "application server" world, so it's highly possible that I'm missing something.
Thanks for any kind reply.
You might want to have a look at WildFly Swarm.
While not documented, there is also the WildFly launcher API. You can see some examples of how it's used in the wildfly-maven-plugn.
Another option would be the application client. Though I'd probably lean towards WildFly Swarm for your use case.
I ended up embedding tomcat 8 within OSGi environment. I really wanted to use the same JVM process for both OSGi and application server (at least to me having 2 separate processes with all the ensuing overhead made no sense), hence embedding tomcat was perfect. Giving up Java EE 7 Full Profile wasn't a big loss since I only needed Web Profile (+ Jersey for web services).
I've written a guide on how to embed tomcat on OSGi here: in case the link will break down, I'll write down here the most important phases:
add to maven all the "tomcat embed" dependencies;
add "felix.service.urlhandlers=false" to config.properties;
Create a new JarScanner from StandardJarScanner where URIs like "http://.extensions:/" are ignored;
Use context.setJarScanner(JarScanner js) method for every context= tomcat.addWebApp(String, String) call;
Make the "tomcat bundle" a framework extension bundle (see OSGi R6 3.15 section);
Register the "tomcat bundle" service via an "extension bundle activator" via the normal ServiceRegistration procedure;
add the tomcat interface bundle package to "org.osgi.framework.system.packages.extra" config.properties (e.g. if the interface of "tomcat bundle" is inside com.acme.applicationserver package add "com.acme.applicationserver"
I won't mark this answer as the correct one simply because my question was related to wildfly. I consider this answer only as a workaround (even though for me this answer definitely solve my issue)

How to call remote JVM class method

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

Java project with executable jars; running and retrieving data from them

At work, I use a Java application (I have located compiled/executable jars on the C-drive). I want to be able to grab some information from this application through code. The application itself probably does not store information, so it must communicate with legacy systems some way, I am not sure how, I have seen traces of a Servlet(?) Hence, I suspect the application also has built-in "encryption"(?)
I do not want to get involved in encryption and login procedures etc., so I am thinking I could just build a Java project around the current executable jars, and launch the application as I usually do (through the "main" entry point, "Start.jar", but then after execution call the functions that I want to (i.e. the application just runs as usual in the background)...
Would that be possible? Is there another way? Can one, for example, hook up to an already executed Java application and issue commands?
What I have tried so far
Downloaded Eclipse, and created a new project
Made Eclipse "reference" external jars (there was a wizard in Eclipse)
Created a new class in my new project, in which I launch the "main" entry point of the "main" executable jar (the structure of all the jars pops up with "IntelliSense"). I have also found out which argument I need to supply to the main procedure using JD-GUI (Java Decompiler)...
It seems that from inside the main procedure a call is made to another procedure, which resides in a different jar, in the debug window of Eclipse I just see an error, which made me doubt that my current approach is viable... Maybe the problem arises because the command is issued from a compiled jar? Could there be an issue with the "class path"? Does this at all seem like a solution? But then again, I have no experience with Java (mostly VBA and some C#).
You can start your JVM for the application with options, which enable remote debugging. Then you can connect the eclipse debugger to this JVM.
http://www.eclipsezone.com/eclipse/forums/t53459.html
Based on your question, I am going to guess that your application does not have a Java API you can code against. That would, of course, be the easiest way. So, if you have not checked, do that first.
Assuming you don't have an API to code against, I think your approach is correct. But it could be hard to do, since you are basically flying blind trying to figure out what the application is doing. Remote debugging might solve part of that problem.
There might be a slightly easier solution, if you are sure it is sending requests across the network. You can use a tool like Wireshark to see what it is creating. Then, you can have your application create requests that look like that and send them to that destination. This assumes of course that the requests aren't encrypted. In that case you are probably out of luck.

Java framework for hot code deployment and scheduling jar file deployment

Using JAVA framework i want to achieve the following task.
hot code(jar) deployment which will perform certain task in an environment
At any time if i update that jar file it should automatically unload old code and load new code
I want to schedule that deployed jar file to perform tasks.
Currently i see Apache karaf/Felix fulfill this requirement but less help available and difficult to manage.
Any alternate framwork i can use instead of using karaf/felix ?
If you aren't going to go the OSGi route, which you basically implied by forgoing Karaf / Felix (and Karaf uses Equinox, by default) then about the best thing I can suggest for you to consider is LiveRebel when it comes out. #Daniel's answer mentioned JRebel, which is outstanding for hot deployment during development but it is not meant as a tool for production systems. Instead you should check out LiveRebel, also made by Zero Turnaround, might be able to fulfill of your needs. Please note that this is a commercial product but they are offering a private beta right now.
[Edit]
Idiotically, I forgot to mention that there's also Knoplerfish, another OSGI runtime which has a BSD style license. Perhaps give that a shot?
Give JRebel a try. It is a great tool.
Note sure what environment you mean (eg. web, desktop, server-side, etc), but...
Working backwards:
3: Scheduled Tasks
You can achieve this in any Java container with the Quartz Scheduler library. This allows you to schedule events in a CRON like fashion.
1-2: Hot Deployment
Then it's a question of where you want to deploy and how to handle hot deployment. Other answers have mentioned JRebel and OSGI which will work. If you want some super quick deployment (eg. save the code and it's available) and have it hosted in a web container ,then use the Play Framework. It uses Quartz do implement Scheduled Jobs in a very nice way.
For example (from the Play docs) :
#Every("1h")
public class Bootstrap extends Job {
public void doJob() {
List<User> newUsers = User.find("newAccount = true").fetch();
for(User user : newUsers) {
Notifier.sayWelcome(user);
}
}
}
JBoss has the hot deploy feature that your describing. However, I'm guessing it's as complicated to configure Karaf. It may be possible to find out how JBoss is achieving it and use the libraries yourself though.
hot code(jar) deployment which will perform certain task in an
environment
At any time if i update that jar file it should automatically unload
old code and load new code
I want to schedule that deployed jar file to perform tasks.
In a nutshell, hot deploy/redeploy is done like that
Use a classloader (java.net.URLClassLoader is a good start), load the jar(s), actually copy the jar somewhere (temp) before loading it
You need some interface implementation, instantiate the class implementing the interface (META-INF in the jar, custom xml, whatever), configure it (props/xml, whatever)
call start() and perform the tasks.
Monitor the jar: some thread to check it each second and compare the last modified time/size
If changed - call stop() and undeploy, may need to wait for threads, etc, start over
There are a lot of frameworks that allow dynamic deploy
The hotdeploy feature of most web containers (like Tomcat or Jetty) allow you to have the behaviour you want, on web applications.
Such an application can be very simple, and essentially just contain your jar.
What is it you need your application to do?

Categories