Java application first run and preload class issues - java

My REST service application is very slow the first time it is solicited.
Apparently the problem is linked with the loading of the java classes and the initialization.
Is there a way to force the application to initialize itself without waiting to the first call ?
Thank you for your time.
Julien.

Related

Running a Thread What Happens after Page Refresh(Runaway Thread)

So I have a Grails App which uses a java jar. Through the grails app the java program can be run. It can also be stopped and paused. My problem is what happens if the user refreshes the page. The thread become a runaway thread and can be accessed. It will keep going until it finishes. Problem being it writes to a database.
My controller just initializes the java program and controls it, using commands from the gsp.
Is there some way of stopping this or is there a better way of doing it?
Any help is much appreciated,
Use services.
You need to put your database code insite a service and call this service in your controller.

Java Applet not working when another web app with applet is active

I have active web app that uses applet to accessing computer COM ports (printing without prompting user). Everything is working fine until there is another web app that uses applet is active. I don't have access to another apps source code.
The problem is I am very noobish in Java Applet-s and I don't even know what might be the problem. Has anyone encountered this issue, or can anyone at least point me in the right direction, where and what should I be looking at, and/or how this problem can be avoided.
Thx in advance
Maybe the life-cycle. There are four methods relating to the life-cylce:
init
start - page is loaded
stop - page is unloaded
destroy
Do all initialisation in start, destruction in stop and everything should be more encapsulated.
Also do not use static objects, and I am even as paranoid to set fields to null when feasible (destroy, stop).

Launch Java application from another application

What I want to do is to have my main Java application to update another java application using Java Web Start and then run the second application "silently" upon user request.
I know Runtime.exec to call javaws and silently import the second application in the cache. I can do that when the first application runs and then I am sure I have an updated copy of the second application. My problem is how to run the second application without showing the Java Web start "Verifying application" window.
Doing some research I see no way to avoid that if I execute the .jnlp. I am wondering whether I can run the second application calling directly the downloaded jar files by passing Java Web Start.
Any Ideas?
Thanks
You have basically two possibilities here:
you can use the JNLP Api service and use the DownloadService;
or use the URLClassLoader and load the remote class.
I don't know if this what you want to do, may be for you it seems as a trick around, forgive me if so...
Why don't you run it as exe, by using process object?
Process process = new ProcessBuilder("C:\\...Desktop\\MyExe.exe").start();
And you can convert your app to exe easily by using jsmooth

JNLP SingleInstanceService Use in Command Line JAR Application

I have a desktop Java application that is run from the command line, which takes in some arguments and performs some actions based on these arguments.
Currently, the application is instantiated periodically, performs its function and then exits.
The issue is that the users are unhappy with the amount of time it takes for the application to initialize. In order to work around this, I thought of simply toggling the visibility of the application when it is finished and setting up some kind of IDLE state.
I was trying to figure out a way to pass in new arguments next time the application needs to do work. I found out about SingleInstanceService and was wondering if it is possible to make this work with my application? It's unclear to me what I need to do so that the Single Instance Service runs on the client PC.
Alternatively, is there another solution for my communication problem? I would rather not depend on File I/O to trigger the application's logic.
Thanks.
AFAIK The JNLP API is available only if you launch your application using java web start (JWS) technology: read more here: http://java.com/en/download/faq/java_webstart.xml
If that is an option for you, oracle has some example of how to use the SingleInstanceService here
Implement and Register SingleInstanceListener. It will be invoked with the main-args when new instances of your application is launched.

Refreshing Swing application with Eclipse/MyEclipse

Say that we are writing a Java Swing application and we use Eclipse or MyEclipse to develop it. In web applications, you make code changes, you save and your ant deployment file takes care of the deployment of the changed files. Then you just refresh or hard refresh the web page and the changes appear there. Can we do the same thing for a Swing applications so that we don't have to close and open the program from the beginning every time we make a change?
I don't think so because you need hot code replacement ! Maybee using another framework.
You can't simply do that because once JVM is started, it loads the class files once and will not reload it untill next loading request. But you can use ClassLoader to load modified class files dynamically.
The following two articles may help:
IBM article on "hot class swap"
"Who Said Runtime Class Reloading Is Hard in Java?"
The first one is in Chinese, but you can look at the code and the result. I think the second article is more helpful for a GUI application.
In MyEclipse you can start your application in debug mode instead of run mode and changes you make will be pushed to the target VM; if changes you make cannot be replaced you'll see a dialog informing you the replace failed and you will need to restart your application. You don't need to place any breakpoints in the application, just starting in debug mode is sufficient.
As Guillaume states above, changes to the class structure will typically not be hot-synched, but changes within existing methods should be fine.
Obviously, how successfully hot-synched changes affect your running application would depend on your application design.

Categories