How can I keep only one instance of the javafx application at the same time?

Hello
How can I keep only one instance of the javafx application at the same time?
Let not the user run the javafx application is running on your PC.

Hello. This program does what you want. You can probably find a better solution.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.*;
import java.nio.channels.FileLock;
import javafx.application.Platform;
import javafx.stage.WindowEvent;

public class Simpleapp extends Application {

    public static void main(String[] args) {

        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        final File file = new File("flag");
        final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        final FileLock fileLock = randomAccessFile.getChannel().tryLock();

        System.out.print(fileLock == null);
        if (fileLock == null) {
            Platform.exit();
        }

        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));

        primaryStage.setOnCloseRequest(new EventHandler() {

            @Override
            public void handle(WindowEvent arg0) {
                try {
                    fileLock.release();
                    randomAccessFile.close();
                    System.out.println("Closing");
                } catch (Exception ex) {
                    System.out.print(ex.getMessage());
                }

            }
        });

        primaryStage.show();
    }
}

Tags: Java

Similar Questions

  • How can I keep my desktop icons in the same place that I left when I restart my computer?

    Icon office locations

    I have the Windows 7 operating system.  How can I keep my desktop icons in the same place that I left when I restart my computer.  Now the icons automatically all go to the left side of my desk when I restart.

    Hello

    References to Vista also apply to Windows 7.

    Difficulty for the desktop icons set in not getting saved after restarting Windows
    http://www.Winhelponline.com/blog/fix-desktop-icons-layout-not-saved-restart/

    -------

    Save and restore the desktop icon layout in Windows NT/2000/XP/Vista (Win 7) - free
    http://www.Winhelponline.com/blog/save-and-restore-desktop-icon-layout/

    Save and restore the desktop icon Layout in Windows Vista (Win 7) - free
    http://www.howtogeek.com/HOWTO/Windows-Vista/save-and-restore-desktop-icon-layout-in-Windows-Vista/

    TIP : right click on desktop - DISPLAY - uncheck AutoArrange and snap to grid
    When you want to restore the icons or the restore will not work. You can
    Recheck those needed later and apply the attribute Align to grid before the
    Auto arrange.

    -------------

    Office catering - free
    http://www.MIDIOX.com/

    Info
    http://www.MIDIOX.com/HTML/desktop.htm

    ------------

    Lock the post Office icons and disable right click
    http://www.techmynd.com/lock-desktop-icons-position-and-disable-right-click/

    I hope this helps.

    Rob Brown - Microsoft MVP<- profile="" -="" windows="" expert="" -="" consumer="" :="" bicycle=""><- mark="" twain="" said="" it="">

  • How can you affect multiple windows open at the same time

    How can you affect multiple windows open at the same time

    You can not.  Only a calendar can have several windows.

  • How can I register several OCX files at the same time

    How can I register several OCX files at the same time. OCX depending on the application files are stored in a network location, IE. R:\HR\whatever\whatever\*. OCX instead of manually record each ocx file, I would be with a simple batch file or an automated script. Anyone have any good ideas?

    Hi James,

    The Microsoft Answers community focuses on the context of use. Please join the professional community of COMPUTING in following the link MSDN forum

    http://social.msdn.Microsoft.com/forums/en-us/categories

  • I can switch between the code sheet and css html, but I would like to see them both at the same time as well as the design window. Right now and can see rather the design and code html or design and css. How can I arrange to see all at the same time, of

    Hello

    My name is Ellie and I am new to Dreamweaver.

    I can switch between the code sheet and css html, but I would like to see them both at the same time as well as the design window. Right now and can see rather the design and code html or design and css. How can I arrange to see everything at the same time, design, HTML and CSS?

    Thank you

    It ends up something like that...

  • Only one instance of the same function call button?

    I have 4 instances of the same key on a single layer on a timeline of the "coverflow_mc" mc named 'ThatsMe_btn_a' to 'ThatsMe_btn_d' IE the same button, same position... He just changing the name of the instance on 4 key frames. Why he calls the function fine for button but not to b, c or d? There is a stop on each of these frameworks and I can see the SWF that it prevents the correct images.

    Picture 7.png

    function thatsMe() {}

    right = new Sound();

    right.attachSound ("right");

    right.start (0,1);

    trace (subTotal);

    }

    _root.coverflow_mc. ThatsMe_btn_a.onRelease = function() {}

    subtotal = 2

    thatsMe();

    };

    _root.coverflow_mc. ThatsMe_btn_b.onRelease = function() {}

    subtotal = 1

    thatsMe();

    };

    _root.coverflow_mc. ThatsMe_btn_c.onRelease = function() {}

    Subtotal = 3

    thatsMe();

    };

    the other buttons do not exist when your code runs.

    to remedy this, make sure that the other buttons exist when your code runs.  You can disable their visible property, so they cannot be seen.

  • How can I not open many tabs at the same time?

    I have a folder in the bookmarks with 17 inside Favorites bar. Today she opens that each of them at the same time, it's new behavior. I need to know how, click on the folder and select one to open. I would change anything I've known since this morning when it was working properly.

    Do you mean when you click on the folder to expand the view of the items in the folder, all items in the folder immediately opened in new tabs?

    What happens normally only if you

    • Hold CTRL while clicking on the file
    • Click the folder with the mouse wheel
    • Right click > open all in tabs

    It is possible that Firefox is confused about the State of the CTRL key, or has problems with your mouse driver software. For the Ctrl key, sometimes press and release that he several times help you clear this point. For the mouse, hmm, you might try shutting down and restart Windows to see if that helps.

  • I have Framework2 Microsoft service Pack 2 and service pack Framework3 on my laptop, can I keep 2 and 3 at the same time or remove 2 to save on memory?

    I have Microsoft Framework 2 SP2

    I have Framework3 servic e Pack 2 on my laptop.

    2 should I keep both at the same time or remove frame

    Thank you.

    SM

    See...

    Should need I even older versions of the .NET Framework on my system after installing .NET Framework 3.5 SP1 [and/or .NET Framework 4.0]?
    http://blogs.msdn.com/b/astebner/archive/2009/04/20/9557946.aspx

  • How can I remove only one instance shared a member of Hyperion Planning

    Hello

    Can someone tell me how I can load the Hyperion Planning IKM to remove only specific instance of a shared member of Hyperion Planning? I tried specifying the shared member, the parent of the shared instance, "Shared" as the type of data storage, and 'Level 0 Delete' operation. Instead of delete just the shared under the specified parent instance, however, the IKM deleted member base, which in turn deleted all instances shared member and member data (thing lucky I work in a development environment!). At one point, I thought I had things works as I wanted to, but now I think I must have confused.

    Is anyone know definitively that a specific instance of a shared member can be removed the planning and, if so, exactly how do?

    Thank you very much.

    Thank you, well it's good news that it's working now.

    See you soon

    John
    http://John-Goodwin.blogspot.com/

  • How can I download all my music at the same time

    How can I download all my music at a time by using the latest version of ITUNES on my Mac.

    Can you give us more details about what you're trying to do?  Download all songs purchased from iTunes Store?  You have iTunes game, and you want to download all the songs from your iCloud music library?  Is this related to the use of an iPod or other device?

  • How can I search for multiple files at the same time?

    In windows XP, after the advanced search mode, I used to be able to search multiple files simply by adding a comma between them like this...

    90025,90028,90094

    and which returns 3 image files I was looking for.

    How can I do this in Windows 7?

    Use OR instead of the comma...
     
    90025 OR 90028 90094
     
    --
    Dave N.
    MS - MVP (Mail)
    Windows 7 Ultimate 64
    "Tziware" wrote in message
    News: * e-mail address is removed from the privacy... *
     
    In windows XP, after the passage in the advanced search mode, I used to be able to
    Search multiple files, simply by adding a comma between them as
    This...
     
    90025,90028,90094
     
    and which returns 3 image files I was looking for.
     
    How can I do this in Windows 7?
     
     

    Windows 7 Ultimate 64

  • How to use more than one address at the same time?

    The Adobe server page that allows entry of a recipients address should not inform users how to add another address?  We separate them by semicolons? Commas?  Or is it still possible to use more than one address?

    Hi orison123,

    You talk about Adobe send? If Yes, you are absolutely right. Who should be written somewhere I had to double check and was able to send to multiple email addresses separated by commas or semicolons.

    Best,

    Sara

  • How can I delete all my emails at the same time?

    My mail folder got on immediately and I would like to know if there is a way to remove all the and start from scratch?  I don't mean remove one at a time, I speak with a single click, and they are all removed.

    What email client?

    With Windows Live Mail, highlight the first email, press the button and highlight the last email. Who should choose all emails. Tap the delete icon, or right click on emails highlighted choose Remove.

    It should be similar for other email clients.

  • Outlook Express - How can I keep email to open in the display pane when I click on it in the Inbox?

    How can I keep email to open in the display pane when I click on it in the Inbox?

    Turn off the preview pane. (View |) Page layout).

  • How can I keep my brush strokes IN the group/game of cutting?

    How can I keep my brush strokes IN the group/game of cutting?

    Double click on go inside the clipping mask with < isolate="" selected="" group="" view=""> and draw inside the isolate group, after finishing your draw press ESC to exit the isolate group.

Maybe you are looking for

  • Satellite Pro R50 - B - feature touchpad does not work

    Hello guys,. I have a problem with the touchpad of a Toshiba Satellite Pro R50 - b pssg1e - basically, I can move the cursor, but I can't use it to scroll or anything else. I encountered this problem of first on windows 8.1 and I thought he was not y

  • Magic Mouse 2 loses connection

    I have a MacBook Pro (mid-2012) with OS X 'El Capitan' 10.11.3 and constantly with the MM2 bluetooth connection failed. I tried all the basic kit to provide solutions by Apple but nothing works. A temporary solution I found, in a blog, was to remove

  • Gate presentation app BlackBerry PlayBook

    Hi all I wrote and published a BlackBerry app that is available in the BlackBerry App World. For the sake of argument let's call him "My App" I have worn since the game for the PlayBook with the same name and want to add it to the BlackBerry App Worl

  • RV016 referral of these two networks

    I've got configuration of port forwarding on a RV016, but it works only WAN2.  All incoming traffic on WAN1 have passed.  I had the impression that the transfer rules apply to both WAN ports?  Several subnets are enabled, WAN2 is allocated a subnet (

  • URGENT blackBerry Smartphones: liquid brand damage on the screen. Pls help!

    Hello I got a new BlackBerry Curve 8520 about a month ago. Today when I rushed to the office, I noticed that there are a liquid dark mark on the middle of the screen. I thought that it would be maybe the screen guard and I removed the screen protecto