Eclipse: Editing the Launch Configuration Parameters

COMP 310  Java Resources  Eclipse Resources

In order to run an application in Eclipse where you need to specify command line input parameters or if you need to explicitly set the working directory, you will need to edit the launch configuration parameters for that application.

The "working directory" is the "root" starting point from which all files in an application are referenced.   If a Java program attempts to read or write to the file system, the working directory is the one used if no directory is specified.    All directories and files in a Java program are referenced relative to the working directory.  

WARNING!! ALWAYS SET THE WORKING DIRECTORY!!

By default, Eclipse will set the working directory to the project directory.   The problem with this is that if you were to run the same Java application from the command line rather than from inside of Eclipse, the application would be started in the "bin" (or "classes", wherever the class files are located) directory rather than the project directory.    Thus the working directory will be the "bin" directory, not the project directory, causing all the references to files in the code to be incorrect.    To remedy this problem, you must set the working directory to be the "bin" directory.

NOT SETTING THE WORKING DIRECTORY IS THE #1 REASON APPLICATIONS SUCH AS RMI FAIL!

See the directions below for instructions on how to set the working directory.

RMI Usage:

Due to the terminal deprecation of the SecurityManager in Java 18+ (see JEP 411: Deprecate the Security Manager for Removal (openjdk.org)), the following work-around is REQUIRED until a permanent solution can be found:

When using a launch configuration in conjunction with a RMI (Remote Method Invocation), add the following line to the Arguments/VM Arguments section of the launch configuration: -Djava.security.manager=allow

Getting to the launch configuration parameter editor:

  1. In Eclipse, open the project you wish to configure.
  2. Click on "Project/Properties" in the main menu.
  3. In the Project Properties dialog box that comes up, select the "Run/Debug Settings" item on the left side.

    Run/Debug Settings

  4. There may be multiple choices of applications to configure if the project contains more than one launchable application, e.g. the client and the server application as shown above.   Highlight the desired application and click on "Edit..." and skip to step 5. below.  Otherwise, continue below.
    1. New launch configurations:  If the applications box is missing the application your desire (e.g. it is empty), it simply means that you've not run it (or anything) yet, so Eclipse hasn't automatically created a launch configuration for it.   To create a new launch configuration, simply click the New... button:
    2. In the ensuing "Select Configuration Type" dialog, select "Application" (unless, of course, you are explicitly configuring an applet) and click "OK".

      Select Configuration Type
    3. The Edit Launch Configuration Parameters dialog will come up, where you should change the default Name from "New Configurations" to something appropriate.   Click the Search...  to select the desired application's Main class.  Continue to Step 5 below.

      edit launch config new  select main type
  5. In the Edit Launch Configuration Parameters dialog box that comes up, select the "(x) = Arguments" tab.

    Edit launch config


    Java 18+: If the application uses RMI, be sure to enter the the required parameter into the VM Arguments panel as described in the warning above.
  6. Setting command-line input parameters:

    1. Fill in the boxes for the input parameters for the application and/or the Java virtual machine (VM).  
      • Typically, for command-line parameters for the "args" input parameter to the
        public static void main(String[] args) {...}

        method, fill in the parameters in the Program arguments box.
    2. Click "OK".

     

    Setting the default working directory:

    There are several ways to accomplish this task, each with their pros and cons:

    1. Relative to the current classpath:
      1. In the Working directory section, click on the "Other" radio button.   
      2. Type in "${project_classpath}".
        • Or click the "Variables..." button and select "project_classpath" and click "OK"
      3. Click "Apply" to save the changes and then proceed below to the next step to create the .launch file.
      4. Pros:/cons:
        • Pro: Independent of project name, so keeps working if project name is changed..
        • Con: Requires that a file in the desired project be selected in the Package Explorer in order to run.
    2. Relative to the current project folder:
      1. In the Working directory section, click on the "Other" radio button.   
      2. Type in "${project_loc}/bin" (or wherever the Java class files are located).
        • Or click the "Variables..." button and select "project_path", click "OK" and then add "/bin" to the result.
      3. Click "Apply" to save the changes and then proceed below to the next step to create the .launch file.
      4. Pros:/cons:
        • Pro: Independent of project name, so keeps working if project name is changed..
        • Con: Requires that a file in the desired project be selected in the Package Explorer in order to run.
    3. Relative to the current workspace folder:
      1. In the Working directory section, click on the "Other" radio button.   
      2. Click the "Workspace..." button and navigate to the desired working directory, typically the "bin" directory.
      3. Click "OK".
      4. Click "Apply" to save the changes and then proceed below to the next step to create the .launch file.
      5. Pros:/cons:
        • Pro: Don't have to click on somewhere in the Package Explorer to run.
        • Con: Dependent on the project name, so stops working if project name is changed.

     

  7. Create the .launch file: ALWAYS create the run/launch configuration file (.launch file) that is then stored in the source control repository so that everyone using it will automatically get the correct launch configuration.  Simple swith to the "Common" tab in the "Edit Configuration" dialog and click the "Shared file" radio button.  Use the default value, which is the project's root folder. This will make a .launch file with the same name as the configuration.

    edit configuration common tab

 

Handy Shortcut for Multiple Launch Configurations:

Once you have made the first launch configurations, the rest are often essentially the same, except for their name and main class.   To make them, simply highlight the desired launch configuration in the Run/Debug Settings dialog and click "Duplicate".   Then just change the Name and Main class for the new launch configuration.   The rest of the settings will already be done.

If the different launch configurations use different command line arguments, such as a API license key or operating mode-determining value, then simply put different values (separated by spaces if more than one) in the Program arguments text box.

 

© 2021 by Stephen Wong