I have made my java application as Single instance application. I have implemented File Lock system for the same.
If the application is already running, I want to show the running application to front. How do I achieve that? How can I acess the running process of that application and show it?
What you are asking for is OS dependent but you can always have your own implementation to do this. You application can listen on a certain port for a bring-to-front command that you can send from the second instance of your application.
void main(String[] args){
if(applicationAlreadyRunning){
// Send bring-to-front message to running instance on a known port
// and exit.
}
}
When a bring-to-front message is received you can do:
public void BringToFrontCommandReceived(){
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
myMainFrame.toFront();
myMainFrame.repaint();
}
});
}
JWS not only provides an x-plat SingleInstanceService, but it also (from memory) pops the application toFront() on newActivation(String[]). Of course if it is not automatic, you can call it explicitly.
Here is a demo. of the SIS.
Related
I am developing an application for a DigitalPersona U.are.U 4500 fingerprint reader and using the U.are.U 2.2.3 SDK Java API.
The sample Java application that ships with the SDK works flawlessly.
However, when I try to do the same thing in my own sample application, the call to the Reader.Capture() method never returns, even though I can see the reader flashing when recording my fingerprint.
Below is a variation on the sample code I have tried with.
Other things I have tried:
Running the capture code in an instance of the class (i.e. not in a static context)
Running the capture operation in its own thread as well, but the results are the same.
Using the CaptureThread class from the demo application
The only difference I can see between my sample and the SDK sample app is that the latter is a graphical application. But why would that make a difference?
Unplugging the device causes the call to fail with an exception. That is about the only way I can get it to return.
import com.digitalpersona.uareu.*;
public class Main{
static Reader r;
public static void main(String[] args) {
try {
// Pick first available reader
ReaderCollection rc = UareUGlobal.GetReaderCollection();
rc.GetReaders();
r = rc.get(0);
if (r==null)
return;
// Open Reader
r.Open(Reader.Priority.COOPERATIVE);
System.out.println(r.GetStatus().status); // Outputs READY
// The following call just hangs and never returns...
Reader.CaptureResult
cr = r.Capture(Fid.Format.ISO_19794_4_2005, Reader.ImageProcessing.IMG_PROC_DEFAULT, 500, -1);
System.out.println(cr.quality.name()); // Just to test
} catch (UareUException e) {
e.printStackTrace();
}
}
}
The last two parameters, the two ints, passed to the Capture method are the resolution and the timeout respectively; passing -1 for the timeout blocks indefinitely. This is taken from the sample application as well.
I finally managed to get an example working.
Strange as it may seem, it only works in the context of a Java GUI application.
So, simply extending a JFrame and starting the reader capture on a separate thread seems to be sufficient.
This requirement is not specified anywhere in the SDK documentation that I can see.
UPDATE
It seems the problem is worse than I initially thought. Not only must the API be called in the context of a Java GUI application, but the GUI must also be in focus, otherwise the capture call simply does not return.
I have verified this with the example SDK applications. The Capture() method does not return if the apps are not in focus. This also applies to the C# examples, where the windows must be in focus, which suggests that this is built into the DLLs that ship with the solution.
This is terrible for our scenario, where we want to develop a local service that a browser can communicate with because, while the browser is in focus, obviously the Java application is not.
I faced the similar issue and it can be fixed by opening a reader in exclusive mode as below,
m_reader.Open(Reader.Priority.EXCLUSIVE);
Refer to below lines from documents,
public static final Reader.Priority COOPERATIVE
Client uses this priority to open reader in cooperative mode. Multiple clients with this priority are allowed. Client receives captured images if it has window with focus.
public static final Reader.Priority EXCLUSIVE
Client uses this priority to open reader exclusively. Only one client with this priority is allowed.
I am currently writing a small Java applet to access HBase data using the REST API. Accessing the data using Java is not particularly difficult, I have done this successfully. When running on a machine in my HDP cluster, the results are perfect. However when running as an applet I get no results at all. (I have chosen an applet since distributing an executable JAR is something my boss wants to avoid)
Having finally found what I believe to be the underlying issue, I have found the following runtime exception: hbase-default.xml file seems to be for an older version of HBase (null), this version is 1.1.2.2.4.0.0-169. My assumption is that this is caused by the fact that my local machine does not have HBase at all. The intention is that users will be able to view their own data from a local machine, and so I cannot expect all users to have HBase (or anything other than a browser)
My question really has two parts:
Is there anyway to get an applet like this to work?
Is there a better alternative to an applet for this kind of work?
Posting my code in case I have made some significant mistake:
public class HBaseConnector extends JApplet
{
private Cluster cluster;
public void init()
{
System.out.println("Applet initialising");
cluster = new Cluster();
cluster.add("hbase_server", 9080);
}
public void start()
{
System.out.println("Applet starting");
Client client = new Client(cluster);
RemoteHTable table = new RemoteHTable(client, "table_name");
Get get = new Get(Bytes.toBytes("key"));
get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("Record"));
try
{
Result result1 = table.get(get);
JOptionPane.showMessageDialog(null, Bytes.toString(result1.getValue(Bytes.toBytes("f1"), Bytes.toBytes("Record"))), "Result", JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception ex)
{
System.err.println("Exception occurred");
}
}
public void stop()
{
System.out.println("Applet stopping");
}
public void destroy()
{
System.out.println("Applet destroyed");
}
}
While I haven't been able to solve this problem for an applet itself, I managed to get the app working by moving over the a JNLP app (JNLP). Given this, I suspect the underlying problem was a permissions issue due to the fact that applets run in a sandbox. This is fine, since I am aware that most browsers are moving away from Java plugins.
Another possible cause I discovered: hbase-default.xml must be in the root folder of the jar.
I wrote a simple Java program to open a specific webpage when the program is run. I need to close it again after a given amount of time. Here is the function I wrote and am using to open the page:
public static void openWebpage(String uri) throws Exception {
java.awt.Desktop.getDesktop().browse(new java.net.URI(uri));
}
Is there a way to close that webpage programatically? I already have another thread acting as a timer, so I simply need to close the webpage once the timer runs out.
I have a Java application that I need to hide from Dock and I also need to be able to interrupt. This is how I handle shutdown:
import com.apple.eawt.AppEvent.QuitEvent;
import com.apple.eawt.QuitHandler;
import com.apple.eawt.QuitResponse;
import com.apple.eawt.Application;
public class MacOSXCustomizer {
public void init() {
Application application = Application.getApplication();
application.setQuitHandler(new QuitHandler() {
public void handleQuitRequestWith(QuitEvent qe, QuitResponse qr) {
if(Main.prepareForExit()) {
qr.performQuit();
} else {
qr.cancelQuit();
}
}
});
}
}
I use my own launcher for Java and I use and Application bundle with my own Java launcher binary. I set LSUIElement to YES which solved my problem with a Dock icon, but then shutdown hook stopped working. My method is called during shutdown (I can log it) but application is terminated even if qr.cancelQuit() is called. It seems like a system is not waiting for response. Even if there is running some operations (2 seconds long) it is not finished. It causes data loss.
I tried to set LSUIElement back to NO and then system cancels shutdown when qr.cancelQuit() is called. No other change was done.
I also tried to create simple Cocoa application which implements only one method:
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
return NSTerminateCancel;
}
it displays dialog "The application ShutdownTest canceled logout." but user session is logged out anyway.
My question is how can I solve to not have icon in Dock and Menu bar and to be able to cancel/interrupt shutdown sequence?
Edit: It is not relevant whether it is a Java application. Native applications have same behavior.
I disscussed it with Apple Tech Support and my requested behavior is not possible. The application designed as an Agent (for instance by setting LSUIElement to YES) is not allowed to interrupt shutdown or logout sequence. System simply doesn't wait for response and don't bother to read and respect return value.
Background application (that does not have Dock icon and Menu bar) is not allowed to interact with user nor cancel his shutdown sequence.
Apple Tech Support says that there is no possibility to hide Dock icon and Menu bar and be foreground application.
I have a Gui-Application and Background-Service that runs on Blackberry device,
I need to implement Sms listener that will invoke some Background-Service methods after it's acknowledges that SMS is arrived to the device, after that it will go sleep again.
I have added class to my application and it's looks like that.
import javax.microedition.midlet.*;
import javax.wireless.messaging.*;
public class SmsListener extends MIDlet implements MessageListener {
public void notifyIncomingMessage(MessageConnection conn) {
}
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
public void startApp() throws MIDletStateChangeException {
System.out.println("Hello from midlet");
}
}
When i want to initiate it it's throws me a SecurityException
Might be i am going not the right way?
How do i implement this kind (Sms listener) of Listener in that kind ( Gui-Application and Background-Service) of Application?
PS: How do i test it in emulator , how do i simulate sms receiving in EMULATOR???
Thank you in advance.
If your MIDlet performs sensitive operations (as opening connections and such) you have to sign it or it will be asking the user for permissions every time, thus providing a horrible user experience. This is true for every phone brand, not only BlackBerries.
In BlackBerry, you can develop your app as a MIDlet, convert the jar to a cod file, and sign it with BlackBerry Codesigning Keys. This format of MIDlet packaged as a cod file is what some people call a RIMlet.
You can find more info on BB forums. Also check this tutorial:
http://supportforums.blackberry.com/t5/Java-Development/Using-MIDLets-on-BlackBerry/ta-p/442789
Why don't you use the example in Oracle documentation ? SMS is JSR 120. Download this pdf about JSR 120 which contains examples.