Restart an Application in JavaFx - java

I want to close and then restart a already running application (automatically) that I am making, by clicking a button or something like that, I want to do that for the purpose of re-launching the application in an other language, I'm new to JavaFx and Java in general, please can you give me a solution for this problem ?

This question lacks details. You did mention a JavaFX application but it is important to know how that application is being deployed. Is it running in the web browser, as a java webstart application, a stand-alone jar, or a self-contained native application? How are you starting the application to begin with? Having the answer to these questions will make it easier to answer your question with specifics.
While the following example is not JavaFX, the approach used here would work for some of the ways in which a JavaFX application can be deployed. One approach for restarting an application that works nicely is to start the application from a script. Inside the script would be a while loop that continually restarts the program based on the program exit code. Here is an example for a bash shell script which starts IntelliJ on a Linux platform:
while true ; do
eval "$JDK/bin/java" $ALL_JVM_ARGS -Djb.restart.code=88 $MAIN_CLASS_NAME $*
test $? -ne 88 && break
done
In this example, the startup script is passing "jb.restart.code" as an application parameter. If IntelliJ wishes to restart, it will return that value 88 as the exit code. The script observes the application exit code, and restarts the application if the value is 88.
This approach works well on most platforms, but requires the application to be initiated via a script.

One solution is to pass commandline and working dir in your start script to your main() method. Using ProcessBuilder you then can restart the appliation. Another possibility is to start the whole application in a custom classloader (e.g. Spring project has suitable classloaders in their source base), you then can basically restart by starting your main in anouther classloader, however you then need proper housekeeping to free threads and resources of the first instance.

retstart.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
if(getOnCloseRequest()!=null){
getOnCloseRequest().handle(new WindowEvent(getScene().getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
//write code to invoke application instance again
}else{
close();
}
}
});

Related

User is able to open the JavaFx desktop application more than once in a Windows machine

I am working on a JavaFX desktop application where the User is able to open the JavaFx desktop application more than once in a Windows machine. From Mac it cant be opened more than once.
The following code is used to launch the application:
Application.launch(ExampleApplication.class, (java.lang.String[])null);
Apart from JUnique library, is there any way to fix this issue ?
Any inputs will be helpful.
Application.launch() should only succeed once per a single application as per documentation. In addition it solely manages the current application and doesn't return until the application exits. If you want to be able to run the application more than once I'd recommend creating a standalone launcher for your application which runs a java process, which in turn runs your application. This way each time the launcher is executed a unique process is started and invokes the application.

loading tomcat using code

im using tomcat 7 , and im trying to do something nice.
i want to have a little swing program to operate my current project (a site)
i wish to have in that swing application a button to run the server , shut the server and such.
i know there is the option of running it as soon as i start the application but i dont want it. i want to have the option to click a swing button and load it.
for that , i need to run the server by code.
is there a way to load tomcat server via code?
A quick solution for this may be Using RunTime you can do this -
Runtime.getRuntime().exec("startup.bat");
similarly, to stop -
Runtime.getRuntime().exec("shutdown.bat");
Assuming both batch files exist in tomcat bin directory.

i want a java application run in background (like skipe or gtalk)

I made a java program for a pop-up from time to time by running a batch file. Application is working fine but after running the application(Batch file) we are able to see the cmd prompt in taskbar(minimize state), but i want the application run in background (like skype or gtalk).
Please help me.
Thank you in advance
Make sure you launch it using javaw (instead of java).
Relevant portion from the first link from the first search here:
This is provided as a helper so that application launches its own GUI window and will not launch a console. Whenever we want to run a GUI based application and don’t require a command console, we can use this as application launcher.
I hope this helps you.
Use a vbs script to invoke the batch script it would hide the window
CreateObject("Wscript.Shell").Run "yourbatchname.bat",0,True

Java service wrapper to create service for Java UI application

I have created a Java application which needs to run as a service. For service wrapper I tried using tanuki and other wrapper softwares.
Now, my application does run as a service - however, it runs as a service under SYSTEM and not under currently logged in user.
My application has a UI, which does not show when the application is running as a service. I doubt that this has happened because the service is running under SYSTEM and not under currently logged in user.
When I run the application (without installing it as a service), the UI does show up and I could see the java process running under the current user's id, whereas it runs under SYSTEM when installed as a service.
Now, there is nothing to my application if there is no UI. I need to application to run as a service when the system boots up and the UI needs to show up as well.
Has anyone tried to do what I am doing here and has faced a similar issue. If so, how did you go about resolving it?
Is there a different wrapper service that I could use and not have this issue.
Tanuki's documentation does explain setting a java application as a service, but does not talk about a java applicaiton with a UI and running it under the currently logged in user.
Update
Just so that folks here could get a bit more idea as to what is it that I am trying to accomplish - the behavior that I am trying to have with my app is similar to how Dropbox or Yahoo Messenger or any other software has which starts with Windows boot and continues to run in the background, allowing the users to interact with it using the system tray icon. So basically all of this has been built in Java/Swing. Now when it has come to deployment of the app - I can run it standalone but running the jar file or by creating and running an executable wrapper for it. However, I want the software to start with Windows boot and continue to run as a service in the background, while allowing the user to make changes and interact with it via lets say bringing up the UI from a system tray icon that it sets.
As of now, it does run as a service successfully. It performs all the actions that it ideally should - there just is no system tray icon to bring up the UI and make changes to its configuration.
Update - 2
Ok.. I tried advanced installer and it does wrap up everything for me to generate an exe installer for windows. Now all I need is to auto-start the software. Can anyone help me with this? I don't see an option in Advanced installer to automatically have the program add itself up in the auto-start.
Link to tanuki's doc http://wrapper.tanukisoftware.com/doc/english/faq.html
Rohan,
Please take a look at the following page:
http://wrapper.tanukisoftware.com/doc/english/prop-ntservice-interactive.html
You will need to mark the Service as interactive and then run as the SYSTEM user in order to get your GUI to show up on the first logged in desktop.
The problem is that Microsoft changed what services are allowed to do starting with Windows 2008 and Vista. This means that starting with these versions, the GUI will always show up on a sandboxed desktop which the user will need to switch over to.
If you want to show a GUI on all desktops, in all versions of Windows, you will need to run the JVM within the desktop.
Cheers,
Leif
I wanted to comment on your question, but I don't have enough reputation to do it. So I'm misusing the answer function now.
I have a similar problem. I used the tanuki wrapper for a while now on a Windows XP machine and the Swing GUI was shown to all users who logged in.
Now I migrated to Windows 2008 Server and configured the current version of the tanuki wrapper like this:
wrapper.ntservice.interactive=true
wrapper.ntservice.hide_console=true
(see documentation here and here)
The Windows service runs under the LOCAL SYSTEM account.
But the GUI is still not shown. Did you find any solution to the problem?
Don't think you should have a UI with a service. Put it in the startup group instead ?
What's does the UI and application do?

Cannot run two instances of java webstart

I am unable to run more than one instance of java webstart at any given time.
For example, I am unable to run both the production & QA instance of an application at once, both of which are launched via java webstart. Additionally, I am unable to run the java webstart cache viewer at the same time as either the production or QA instance of my application.
I am however able to run any of the above three webstart launches when they are run in isolation of each other. When I try to bring up a second option, I see the 'Java Loading...' screen which then disappears and nothing happens.
Additionally, I have tried to delete the webstart cache (via the java webstart cache viewer) and I receive the following error regardless of which JRE I point to:
"Bad installation. Error invoking Java VM (execv)
'path to my javaw.exe'"
I expect both the problems I mention above are interlinked. I do not believe I have changed any configuration recently and I have been happily running java webstart for years.
Has anyone seen such a problem before?
Thanks,
Jack
EDIT: When the second instance of webstart attempts to run, during the display of the 'Java Loading...' screen I can see in the task manager that a new javaw.exe process is spawned. This process almost immediately dies though. I'm not sure how to inspect the failure in that process, but I expect it is similar to the failure when trying to clear my cache through the webstart cache viewer.
You may be able to use javaws from the command line to run a second instance in -offline mode. The verbose option is handy, too.
javaws -offline -verbose MyApplication.jnlp
I think it is because both instances of the application use the same folder as current working directory. I do not remember exactly but it is somewhere under user home and the folder contains the application name or something...
So, if this is correct the solution is to change the application name like "My Application - QA" vs. "My Application" used on production.
The name is somewhere in jnlp.xml.
The reason may be the startup parameters for client java/javaw, which do not allow to run more than one instance of Java. For example because of set debug port. These parameters can be set in the command line or in the Java Control Panel -> Java -> button View.

Categories

Resources