Make program sleep until a windows task has completed - java

I have a small program that will backup some important files then uninstall a program. once that is done it will then install a bunch of new stuff and backup the old files and configure everything. It works fine but the problem is when i run the program it just loads up a bunch of installers and restores the backup files before the programs have even installed.
I know this is happening because the java isnt responsible for the install of the programs its windows job but is there a way to delay the program so that it wont go onto the next step until the last 1 has completed? i could guess roughly how long it takes to complete a step and use thread.sleep() but i would like something more accurate. Is there a way to somehow find out from windows when it has finished doing a task?

If you start the installer using the ProcessBuilder then you get a Process object returned when you call start().
If you then call waitfor() on the Process your thread will wait until the installer is finished.

Related

Netbeans RCP application not working outside the IDE when built as OS independent zip

I am building a statistical analysis application on Netbeans RCP for solving tests faster and with less effort https://github.com/PaulMaxAvalosAguilar/Statistikos-Klubas.
The thing is very simple:
1.-There's a module called TrabajosViewer which uses the nodes API to display workspaces where you can organize your samples with some meaningful name, first you create a workspace and then you add some data to it.
2.- You open editor top component which calculates some descriptive satistics stuff for all the sample you entered
3.- All stuff you need for a test is done! Samples are stored in an embedded H2 database(datos module) and results are calculated each time you click on a TrabajosNode.
However when I was testing the app I had to add the following sample:
https://1drv.ms/t/s!AkZmosJJMvdIu3c_IiVkD6JAVVgk (3344 elements); as the app had multithreading capabilities everything was fine, except that after building a release as OS independent zip the app froze from the swing Top Component which was very rare as that wasn't happening on inside Netbeans IDE. I think it has something to see with the build script since I built a Gradle version an everything worked fine https://github.com/PaulMaxAvalosAguilar/Statistikos-Klubas2
If you have an app - any Java app really, not just NetBeans RCP - and it freezes then what you want to do is obtain a thread dump. The thread dump will almost always give the clue as to why there's a freeze.
Java has 5 or 6 different ways to obtain a thread dump for a running application (just google it). Out of these methods, the one preferred is the so-called Ctrl-Break method because it is the one which gives most information. The downside of the method is that you need to have started your application from a console window. But if you can consistently replicate the freeze, then I don't think that's a problem for you. Here's what you would do if you are on Windows:
Start cmd.exe
From the command window, start your application, e.g. bin\sillyapp64.exe.
Wait for the freeze to happen on your application
Now press Ctrl-Break in the command window. This will give you a thread dump printed into your command window.
Upload the thread dump as part of your question. (or host it somewhere if it is too big)
Instructions for Linux/MacOSX are similar albeit in step 4 you would instead send your process a QUIT signal using the kill command.

Java: How to terminate the process created with ProcessBuilder("startx", "firefox")

How do I terminate the process I created with ProcessBuilder("startx", "firefox")? process.destroy() and process.destroyForcibly() don't work.
I'm starting a firefox kiosk, but only once the rest of the Java application is up and running (so that I can keep quick boot times and not have to wait for X to start). Is there a better way to do this than "startx firefox"? Is there a way of passing something to x to get it to close.
I need to restart the software to be able to update it. Obviously I'd rather not have to reboot the machine just to update it.

Java Process running from Windows Powershell sometimes randomly stops until pressing Ctr+C

I am running a Java process with several threads from a Powershell on Windows Server in Admin Mode.
Sometimes it randomly freezes until I press Ctrl+C, then it just picks up the work again.
Has anyone ever come across this problem and point me to where to look at?
If you start the process with Runtime.exec(..), it is sometimes necessary to read Bytes from the Process.getInputStream() resp. Process.getErrorStream(), else the process blocks, when it tries to write more to std-out (or std-err) than a certain buffer size.
I had this problem often, when starting Shell scripts.
You can create a Background Thread to read periodically from these streams.
Alternatively and more easy, you can use the ProcessBuilder class to start the shell process and use "inheritIO()" method.

About program running independent from command line under unix

I am writing a java program that runs under unix.
It would like run forever. But when I start it from command line, I have to leave that window open always until the program stop.
Could anyone give me some idea about how can i run it at back end? Just start it from command line then I could close that command line.
Thank you very much.
If you don't want to "daemonize" it you can just use nohup:
$ nohup your-program &
$ exit
and your-program will continue to run in the background until it finishes.
You're asking about making your program a "daemon". Check out these links about daemonizing java programs, and this one about daemonizing any process in linux.
...Another option is to use the "screen" utility. Its a little tricky if you've never used it, but you can do things like launch a job in a terminal at work and easily reconnect to the same terminal from anywhere else to check on the status of the job. I use it for connecting to servers where I run long-running jobs. Without using screen my process would die if my local machine crashes, or the power goes out, or fire, etc.

Prevent a user from terminating a java program?

I want to install a monitoring system on a computer (the program is a jar file) and run it on start up every time any user logs on. However, I don't want the user to be able to terminate it since then it won't be able to be monitored any longer.
We have tried several ways:
Installing it as a service - the problem here is that our program doesn't work any longer; it can't connect to the computer that's monitoring it. We used "Yet Another Java Service Wrapper" for this, and looked into some other wrappers as well that could help us install it as a service.
Running the program on start up (using the folder startup), but not giving the basic user the privileges to edit/delete/mess around with the files. However, this seems to slow the whole computer down? This doesn't happen when we run the bat file executing the jar directly. Another issue with this is that the user can just go to the task manager and kill the java process.
We tried a variation of the previous one to solve the issue of the process being killed, by having another process. One will spawn the other and these 2 processes will keep tabs on each other. If one terminates, the other detects it and runs it to start it up again. Although it can have issues if the user is fast enough in killing both processes before either is respawned again, the bigger issue is that it sometimes has problems with connecting to other computers. We didn't have this problem when it was just 1 jar.
Does anyone have any idea on how these problems can be solved?
The context here is windows, but if you have suggestions for linux and mac that would be nice too!
Way to go is to run the program as a service. You should investigate any trouble between your application and your system's firewall. If you have windows firewall activated, you should add an exception for java.exe or javaw.exe.
In order to give elevated privileges to your program, you can set the service to run as another user. You can do this from the "Log on" tab in the service properties.
You'll want to have the program started under a user with elevated permissions. On WIndows this would the the Administrator, linux would use root. On Windows, its likely that you will need to start it as a service. But I really don't know why that would hinder the network communications.

Categories

Resources