Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:20:58+00:00 2026-05-27T15:20:58+00:00

I had an rcp application which runs for only first run, when a user

  • 0

I had an rcp application which runs for only first run, when a user attempts to re-execute the application, second instance behaves as a client which encodes and sends its arguments over the socket to the first instance which acts as a server and then exits silently. The first instance receives and decodes that message, then behaves as if it had been invoked with those arguments.

so far so good i made internal protocol specification for passing arguments between two instances.

I could not bring the first instance(RCP application) to front. It is in minimized state only,

this is in continuation to my previous question

the change i made to previous post is start method of application class

public Object start(IApplicationContext context) throws Exception {
        if (!ApplicationInstanceManager.registerInstance()) {
            return IApplication.EXIT_OK;
        }
        ApplicationInstanceManager
                .setApplicationInstanceListener(new ApplicationInstanceListener() {
                    public void newInstanceCreated() {
                        Display.getDefault().asyncExec(new Runnable() {
                            public void run() {
                                System.out.println("New instance detected...");

                                //Display.getCurrent().getActiveShell()
                                        .forceActive();// this gives null
                                                        // pointer exception
                                                        // hence commented
                            }
                        });
                    }
                });
        Display display = PlatformUI.createDisplay();
        try {
            int returnCode = PlatformUI.createAndRunWorkbench(display,
                    new ApplicationWorkbenchAdvisor());
            if (returnCode == PlatformUI.RETURN_RESTART)
                return IApplication.EXIT_RESTART;
            else
                return IApplication.EXIT_OK;
        } finally {
            display.dispose();
        }
    }

below line is stopping me to bring Application to front

Display.getCurrent().getActiveShell().forceActive();

generates null pointer exception at getActiveShell()

how can i maximize the previous instance or bring it to front

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:20:58+00:00Added an answer on May 27, 2026 at 3:20 pm

    I wrote an instance manager to restrict my RCP to a single instance.

    Here’s the code that goes in Application.java, in the start method:

        if (!ApplicationInstanceManager.registerInstance()) {
            return IApplication.EXIT_OK;
        }
    
        ApplicationInstanceManager
                .setApplicationInstanceListener(new ApplicationInstanceListener() {
                    public void newInstanceCreated() {
                        Display.getDefault().asyncExec(new Runnable() {
                            public void run() {
                                if (DEBUG)
                                    System.out.println("New instance detected...");
                                Display.getCurrent().getActiveShell().forceActive();
                            }
                        });
                    }
                });
    

    Here’s the listener interface:

    public interface ApplicationInstanceListener {
    
         public void newInstanceCreated();
    
    }
    

    And here’s the Manager class:

    public class ApplicationInstanceManager {
    
        private static final boolean DEBUG = true;
    
        private static ApplicationInstanceListener subListener;
    
        /** Randomly chosen, but static, high socket number */
        public static final int SINGLE_INSTANCE_NETWORK_SOCKET = 44331;
    
        /** Must end with newline */
        public static final String SINGLE_INSTANCE_SHARED_KEY = "$$RabidNewInstance$$\n";
    
        /**
         * Registers this instance of the application.
         * 
         * @return true if first instance, false if not.
         */
        public static boolean registerInstance() {
            // returnValueOnError should be true if lenient (allows app to run on
            // network error) or false if strict.
            boolean returnValueOnError = true;
            // try to open network socket
            // if success, listen to socket for new instance message, return true
            // if unable to open, connect to existing and send new instance message,
            // return false
            try {
                final ServerSocket socket = new ServerSocket(
                        SINGLE_INSTANCE_NETWORK_SOCKET, 10, InetAddress
                                .getLocalHost());
                if (DEBUG)
                    System.out
                            .println("Listening for application instances on socket "
                                    + SINGLE_INSTANCE_NETWORK_SOCKET);
                Thread instanceListenerThread = new InstanceListenerThread(socket);
                instanceListenerThread.start();
                // listen
            } catch (UnknownHostException e) {
                EclipseLogging.logError(RabidPlugin.getDefault(),
                        RabidPlugin.PLUGIN_ID, e);
                return returnValueOnError;
            } catch (IOException e) {
                return portTaken(returnValueOnError, e);
    
            }
            return true;
        }
    
        private static boolean portTaken(boolean returnValueOnError, IOException e) {
            if (DEBUG)
                System.out.println("Port is already taken.  "
                        + "Notifying first instance.");
            try {
                Socket clientSocket = new Socket(InetAddress.getLocalHost(),
                        SINGLE_INSTANCE_NETWORK_SOCKET);
                OutputStream out = clientSocket.getOutputStream();
                out.write(SINGLE_INSTANCE_SHARED_KEY.getBytes());
                out.close();
                clientSocket.close();
                System.out.println("Successfully notified first instance.");
                return false;
            } catch (UnknownHostException e1) {
                EclipseLogging.logError(RabidPlugin.getDefault(),
                        RabidPlugin.PLUGIN_ID, e);
                return returnValueOnError;
            } catch (IOException e1) {
                EclipseLogging
                        .logError(
                                RabidPlugin.getDefault(),
                                RabidPlugin.PLUGIN_ID,
                                "Error connecting to local port for single instance notification",
                                e);
                return returnValueOnError;
            }
        }
    
        public static void setApplicationInstanceListener(
                ApplicationInstanceListener listener) {
            subListener = listener;
        }
    
        private static void fireNewInstance() {
            if (subListener != null) {
                subListener.newInstanceCreated();
            }
        }
    
        public static void main(String[] args) {
            if (!ApplicationInstanceManager.registerInstance()) {
                // instance already running.
                System.out.println("Another instance of this application "
                        + "is already running.  Exiting.");
                System.exit(0);
            }
            ApplicationInstanceManager
                    .setApplicationInstanceListener(new ApplicationInstanceListener() {
                        public void newInstanceCreated() {
                            System.out.println("New instance detected...");
                            // this is where your handler code goes...
                        }
                    });
        }
    
        public static class InstanceListenerThread extends Thread {
    
            private ServerSocket socket;
    
            public InstanceListenerThread(ServerSocket socket) {
                this.socket = socket;
            }
    
            @Override
            public void run() {
                boolean socketClosed = false;
                while (!socketClosed) {
                    if (socket.isClosed()) {
                        socketClosed = true;
                    } else {
                        try {
                            Socket client = socket.accept();
                            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(client.getInputStream()));
                            String message = in.readLine();
                            if (SINGLE_INSTANCE_SHARED_KEY.trim().equals(
                                    message.trim())) {
                                if (DEBUG)
                                    System.out.println("Shared key matched - "
                                            + "new application instance found");
                                fireNewInstance();
                            }
                            in.close();
                            client.close();
                        } catch (IOException e) {
                            socketClosed = true;
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had a question from a client which stumped me. They are using IIS 6.0
Had a page that was working fine. Only change I made was to add
I have an Eclipse RCP application. When the application is started I want to
Had to deploy some PHP code onto a shared server with only PHP5.2.8. All
We are developing an Eclipse RCP application. We decided to use SVN for revision
I am trying to apply a thirdparty LAF to a NetBeans RCP application (not
I'm a beginner with Eclipse RCP and I'm trying to build an application for
I made a simple Eclipse RCP client, and now I want to turn it
Had for an photo art project using two images. The first image would be
My colleagues and I are building a new RCP application and trying to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.