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.
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.
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:
VM Arguments
panel as described in the warning above.
There are several ways to accomplish this task, each with their pros and cons:
- Relative to the current classpath:
- In the Working directory section, click on the "Other" radio button.
- Type in "${project_classpath}".
- Or click the "Variables..." button and select "
project_classpath
" and click "OK"- Click "Apply" to save the changes and then proceed below to the next step to create the
.launch
file.- 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.
- Relative to the current project folder:
- In the Working directory section, click on the "Other" radio button.
- 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.- Click "Apply" to save the changes and then proceed below to the next step to create the
.launch
file.- 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.
- Relative to the current workspace folder:
- In the Working directory section, click on the "Other" radio button.
- Click the "Workspace..." button and navigate to the desired working directory, typically the "bin" directory.
- Click "OK".
- Click "Apply" to save the changes and then proceed below to the next step to create the
.launch
file.- 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.
.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.
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