Audio playback not working correctly - java

I'm developing a program that simply monitors a webpage for changes, and plays a sound file. I implemented similar to this answer using SwingUtilities.invokeLater and JOptionPane to make sure sound file is looped continuously.
Program works correctly. Now I would like to place it in crontab (on my Ubuntu Machine) and have it run every 30 minutes. That's where things stop working. I searched for why, and it looks like JOptionPane should be invoked from initial thread (which is the crontab process?). I understand that I just need to prevent main method from ending, but what would be the best approach (DataLine.Drain() ?).

You are trying to display something without making sure a display server is present and accessible (which is not a given for a process started by cron).
You can specify a display server by setting the DISPLAY variable to :0. For example:
* * * * * export DISPLAY=:0; YOUR_PROGRAM
See also https://stackoverflow.com/a/25182822/942774

Related

Java processes remain alive after CRON

I created a jar that grabs an image from a URL (webcam of my favorite beach) and saves it to a file using the timestamp as a name. Yes, I know there are programs to do this - I'm a propellerhead and I wanted to do it myself. It works like a champ.
Then I scheduled it to run every minute with CRON. Again, worked like a champ.
* * * * * java -jar /apps/clearcapture.jar
After running for several hours (Mac OS 10.9) I found the machine running at a crawl. Activity Monitor revealed MANY java processes. Without counting them, I would guess that there was one instance for every time the image was captured.
So, what's going on here? I expect that when the jar is done, its parent Java instance would go away.
Thanks,
Chris
At the end of main method of the main class, you should call System.exit to explicitly exit the program. For better exception handling, you should capture all exceptions, log them into a proper log and call System.exit with system error output.
I resolved it by explicitly closing the HTTPUrlConnection at the conclusion of the download.

restart java.exe from an applet

I have an applet packaged with a third part dll (from JTwain). My applet scans documents from the TWAIN compatible default printer. The applet fails on a paper jam and won't recover. The user navigates away from the page and the applet is destroyed. When returning to the page it fails again. Closing the browser (which kills java.exe process on the pc), and then returning to the page clears the problem and everything works.
I want to restart everything without requiring users to close down the browser. I've added a GUID query string to the URL's from which the applets resources are loaded - so I know nothing is being cached. I've checked in the windows task manager and there is no process created by the dll, it's all happening within the main java.exe process. I tried wrapping the scanning process in a thread so I could interrupt it in the stop or destroy methods (just in case the applets thread weren't stopped when the applet was destroyed), but that didn't work.
Any suggest would be greatly appreciated. Ideally I'd like some way to restart java when the applet unloads (but I doubt that's possible).
UPDATE
I've spent a couple of days trying to identify what causes the applet to fail. I still don't know :(
When the paper jam occurs something (not my code), is producing a couple of popups. The first alerts the user of the jam, and can be closed by clicking the OK button. The second says 'reading from device' and hangs. It cannot be close with the red, close window, icon in the top corner - I kill it from the task manager and windows asks to send a report regarding the 'non-responsive program'. I assume these popups are produced by the dll. And given that the second hangs, my assumption is that a thread started by the dll has hung while retaining a lock on some component of the TWAIN application. I get
com.asprise.util.jtwain.JTwainException: Failed to open the specified data source:
Source: TW-Brother MFC-9970CDW LAN Thrown
..when I try to access the scanner.
I'm at a bit of a loss as to how I can get more information. I'm testing my applet on a windows virtual pc (so as to use ie7), and don't have a method for step debugging in this environment. (And it's crashing on third party code for which I have no source anyway)
I see only two practical options here:
Use an API that handles paper jam without problems. Of course, that is easy to say (get robust API), harder to find.
Launch the app. free floating using Java Web Start. If it freezes up, the user can kill it and click the link for another instance in a new JVM. Or the applet might also call BasicService.showDocument(URLof.jnlp) if it can detect a problem with the DLL and is not itself frozen.
Of course, you should also report the bug to the ..Asprise(?) developers. The optimal solution would be to have the problem fixed at its source. Anything we do here is a 'workaround'.

How to Remotely Block and Unblock any application on Windows

I am creating a program using Java Sockets in which I capture the client desktop and send messaging to client. Its working properly but now I want to block Client applications like Notepad, MS-Word, etc.
How can I do this?
Thanks.
It is hard to do using pure java API.
I do not know what do you mean when you say "block". The easiest way is to check from time to time running processes and kill one named "notepad" by executing taskkill from java.
If you wish to achieve effect of inactivity of application, i.e. user sees the notepad but cannot type you can do the following.
You have to check which application is on front. There is no clean pure java solution for this but you can probably write VBScript or JScript that does this task and run it from java. Once you detected that notepad is on top create transparent window (or even probably half-transparent window) that occupies full screen. Bring it on top. User will not be able to type into notepad because your window is on top but will see it.
Here is reference how to create transparent windows: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Good luck.

Using an empty file to have one application instance

There's probably other ways of doing this but I'd like to use an empty file to have one instance of an application running at a given time. This would be done by creating the file when the application is launched and have other application instances exit as soon as they detect the file.
The trouble with this approach is that the file can remain if the application stops unexpectedly and a ShutDownhook is proving unreliable.
How would you go about making this work as intended?
Bind to a high numbered port but don't listen. Two programs can't bind to the same TCP port on the same machine. Very cross-platform but still somewhat of a kludge.
Create the file and keep it open with an exclusive lock (that is, don't pass FILE_SHARE_READ, etc). When the second instance starts up, it tries to open the file and if it fails it means the first is still running.
If the first crashes, then Windows will automatically close all file handles and so the second process will come along, see that the file is there but since it can open it then it knows the first has crashed (this technique could also be used for a special dialog, "I see the previous instance of this application crashes, would you like to restore your last session?" or something)
You could always wrap your Java program in another program (doesn't need to be complicated - could even be a shell script) that would detect abnormal exit and delete the file. e.g.
if(!`java MyProgram`){
rm lockFile
}
This may be a little much. But you can start a ServerSocket and bind to some arbitrary port that each application knows. If the port is available the application wins to start up, if not a binding exception is thrown and the application gracefully stops.
There is probably a better way to do this than using a file but with that approach you could write a timestamp to the file and update it at regular intervals using a Timer. Then when your program starts it could compare the timestamp in the file with the current time and quit if it is too close. This guarantees that your program will be able to restart no matter how it was terminated.
Assuming the lock will be released when the application exits and deleting the file on startup does the trick. See the link below.
http://jimlife.wordpress.com/2008/07/21/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/
P.S: I've tried using delete when the application exits but it seems to fail.

Getting scp's status bar to appear in a Java window

I'm writing a program that uses scp to copy files in a bigger java program. As it stands now, the program freezes up while the scp is copying the file, which can take a few minutes, so I'd like to be able to display the progress of the scp or at the very least get the terminal window with the scp progress to show up! Any suggestions?
scp writes to STDOUT and flushes the output. If you pipe the output, you may be able to read the output from the pipe, but my guess is that 1) scp wont print the progress bar to a pipe or 2) if you can do 1, the output will only make sense if you re-implement a pty or similar in a java window.
However, you could experiment with an expect-like module calling scp and mimic how the terminal works and you may be able to fake scp into thinking its being run as a terminal.
Finally, (and this is probably the best answer), don't use scp, find a java library to import that directly links into openssh's protocols for transferring files with scp.
When you say, "the program freezes up", my guess is that you mean that a Swing UI freezes up. If so, launch scp in a background thread and show the user an indeterminate progress bar.
Invoke the "launch scp" using SwingWorker to get it of the main thread and let the GUI update again.
To get actual progress you will need to investigate the characters printed by scp. I believe it maintains an ASCII progress bar, you can then replicate in Java. You may want to add "-v" to scp to let it be more talkative.

Categories

Resources