How do I make a custom windows service? - java

Actually I am having one java program. That returns "Hello World". I converted it to jar file. After that I changed the file type as .exe with the use of iexpress in my windows XP default sw.
I used the sc create command, for creating new service. I created the windows service successfully. But the thing is I am not able to run this. It is throwing the following error.
This error is coming not only for this javaservice.
I tried to run the chrome.exe as windows service, That time also it is throwing the same error.
Is it windows constraints? or Am I making mistake? Expecting the solution.,
Thanks in Advance...

You cannot just assign any arbitrary .exe file as-is to run as a service. There are specific API functions involved that services must use to interact with the Service Control Manager (SCM). That is why you are getting the errors - those .exe files are not using those APIs to interact with the SCM.
If you are not writing code specifically for SCM interaction (Java does not support creating Windows services), then all is not lost. In some situations (Chrome NOT being one of them!), you can use a separate wrapper to host non-service apps and handle the SCM interactions on their behalf:
Service wrapper
For Java apps, there are a few wrapper projects available:
Java Service Wrapper
Yet Another Java Service Wrapper
ow2

Related

Call java application launched from another container

Here's situation. I have one container with php code and php-fpm - this is my application container. Sometimes main application calls java application - jar file. So I decided to split those technologies and make seperate java container with this java application. Now I need a way to call jar file launched inside another container. One way is rebuilding java applicaton to support REST api, but it takes time so is there any other possibility to solve this problem?
You could take a look at Java Remote Method Invocation (Java RMI), but to be honest, I have no idea if it's possible to to this invocation not using java.
https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/hello/hello-world.html

Can I open a REPL in the context of a Spring Web Application?

I'm working on a web application based on Spring MVC and Hibernate on Tomcat 8 (both for deployment and local development). The IDE is Spring Tool Suite (which is based on Eclipse).
I want to open a REPL (read-eval-print-loop, like Groovy's, Python's, Ruby's, etc) in the context of my application (while it's running on Tomcat locally), to speed up development by shortening the code -> test feedback loop.
That is, I want to be able to open a shell in the command line (or inside Eclipse) and do something like:
ClientDAO clientDAO = getAutowiredDAOFromSpringSomehow();
Client client = clientDAO.findByID(100);
client.setName("Another name");
clientDAO.save(client);
I can work around this a bit by setting a breakpoint somewhere in a controller and use Eclipse's debugger Display tab to execute arbitrary code, but this is a bit impractical and uncomfortable.
I'm open to using Groovy or Scala's shell if it's more convenient (I obviously still need access to my objects, though).
What are my options?
You may be able to do it using Spring Shell, JShell or BeanShell
Here's a project to embed a repl in an android app using BeanShell
I don't know if it's useful for your use-case but theoretically it should be possible to do this using CRaSH. It's a Shell like Bash on Linux but for your Java-Application and it's possible to create your own commands.

How to build a command line interface in java for an existing web based application

I have created a web based application using JSP and Servlets and the application uses an SQL Server DB as its backend.
The architecture is like this:
I have all my business logic in a jar file
I have created my views using JSPs and am using servlets to interact with my business logic jar
The jar connects to the database to persist and hydrate information, which is relayed to the JSP by my servlets.
My web application runs on a remote Tomcat server.
Now, I have been given a new requirement. I have to create a command line interface, where I should be able to specify a list of commands and hit enter (or alternatively, create a set of commands and save it in a .bat file or something, and run it), so that my application performs the necessary actions. Basically, I have to create a command line interface, which can be used along with the GUI i already have (JSPs).
I am totally new to this. Can anyone throw light on where and how I can start?
Any little help is greatly appreciated.
EDIT
This is what my web application does. User can see a list of test scripts (written in Selenium WebDriver). He can choose script(s), choose a host on where to run them from, and click "Run", and the test executes on the said machines.
Now, I want a command line interface, which will eliminate the need for the GUI. Let's say, I simply want the user to be able to type a command like "execute My_Script_1", and the script should be executed.
The test scripts, the selenium drivers, everything reside on the App server.
My command line interface should be able to work on Windows command prompt.
Thank you.
Are you using Spring?
Can you specify, what exactly your CLI should do?
You may do, what Thomas said.
You also may use template engines like Velocity.. To form your output.
Use some kind of JavaCurses-like library to make your output... Look well.
Specifying commands...
Hm.. think about your business logic what exactly you are showing to user.
Remember webapp ui is webapp ui. Console ui is different. And user expects different behaviour
So commands like
show goods category="for kids"
Will be great.
Also don't forget about different help commands
yourJarName.jar --help / -h and etc
If your are want to write application with interactive mode... think about help command there.
You say you have your business logic in a JAR.
Why not starting another project with this JAR as a dependency and build it as an executable jar ?
Then simply use System.in and System.out to interact with the user.
EDIT :
So your application is hosted. Do you have an API like REST or SOAP or any other ?
Then you can build a client reading a string that the user has written, parsing it and calling the right service in your API.
I see two options:
Create a client-side CLI that generates the same data your server
receives. In other words, you don't modify your server code, and you create a
client-side CLI module (with jQuery for example) that parses the command lines and sends
exactly the same thing your actual GUI sends.
Set up a text area in your web app (decorated as a CLI) that reacts
on each Enter key pressed, and sends the line(s) to your server. On
your server, you can create a utility class (say CLIParser.java for
instance), and use Args4j to parse the received command,
validate it and run it.
Have you looked at Primefaces terminal? http://www.primefaces.org/showcase/ui/misc/terminal.xhtml
You data structure looks simple enough. Also you mentioned you designed your application the way the business logic is separated from the front end.
In this case you may consider exposing your business logic as a REST based WebService. It should not be that hard since you have layered structure in your application.
Looks like a few methods:
list scripts - returns a list of available scripts list hosts
returns a list of available hosts run script(scriptName, hostAddress)
runs script scriptName on a host with on address hostAddress possibly returns the results if your application supports this
All three look like a good candidates for GET methods.
You may consider Jersey or Resteasy or another framework.
You can find plenty tutorials for both of them. Take a look for example here.
From your command line application you can make calls to your web service in different ways. Just because I used to work with Jersey JAX-RS implementation most of the time, I found use of Jersey client(the latest stable version) the most convenient. Here you can find a short tutorial how you can do it from your command line application with Jersey client. JBoss also has a client API as a part of their framework(also fully certified JAX-RS implementation). You may even decide not to use any client API and do all the work manually utilizing HttpURLConnection, but I would not recommend. There is no big difference in using client API or do all the work manually with HttpURLConnection for the simple service, but you never know when your application becomes not that simple because of new requirements your client could not think of at the beginning.
Hope that helps

Jar files required to Invoke Webservices

I have ported jamvm to my armel device. I am able to run various swing based and communicate to other systems via socket programming method. But when I try to call a webservice I get many NoClassDefFound exceptions.
I placed jaxws-api.jar file to remove some such exceptions which directed me to other class definition not found exceptions.
Can someone please tell which arfe all the necessary jar files are required to be present during runtime in order to invoke a webservice on a server machine.
Thanks in advance.
If you are using net beans IDE to develop the restful service it is very essay. Just follow these instructions all library (including Jersey) will be added to your project. If you need to get the library separated take the lib folder in project path.
http://netbeans.org/kb/docs/websvc/rest.html
p.s : You don't need to create DB though.

Executing a class in remote jvm

I have a small test class that I want to run on a particular jvm that's already up and running (basically it's an web application running on Tomcat) . The reason I want to do this is I want to execute a small test class (with the main method and all) within that jvm so that I get the same environment (loaded and initialized classes) for my test class.
Is it possible to indicate that ,say through a jvm parameter, that it should not initialize a new vm to execute my class but instead go and execute on the remote vm and show me the result here, on my console. So the local jvm acts as a kind of thin proxy ?
I am not aware in case there are some tools that should make this possible .Also heard somewhere that java 6 jvm comes with an option like this , is that true ?
Please help me.
Thanks,
After reading this question and the answers, I decided to roll my own little utility: remoteJunit
It is lightweight and dynamically loads classes from the client to the server JVM. It uses HTTP for communication.
You might want to take a look at btrace. It allows you to run code in an already started JVM provided you don't change the state of the variables inside that JVM. With this kind of tracing, you might be able solve your problem in a different way. Not by running extra code in form of a new class but by adding safe code to and existing class running inside a JVM.
For instance, you might System.out.println the name of the file when there is a call to File.exists.
You might find JMX useful. Register an MBean in the server process. Invoke it with visualvm (or jconsole). (tutorial) Never tried it myself, mind.
RMI would also do the magic.
http://java.sun.com/javase/6/docs/technotes/guides/rmi/index.html
Make your web application start an RMI registry and register your service
beans there.
Then in other JVM you can run a program that queries the RMI registry
started by your web application for the services you want to verify
and you are done.
I assume "small test class" is basically some debugging code you want to run to monitor your real application, which is deployed remotely on a Tomcat. If this is the case, you should connect your Eclipse debugger remotely to the Tomcat instance, so you can set a breakpoint at interesting locations and then use the Display view of Eclipse to run any arbitrary code you might need to perform advanced debugging code. As java supports Hot Code Replacement using the debug mechanism, you can also change existing code on the remote side with new code at runtime.

Categories

Resources