How to write a script to run a Java program and set variable values?

I have written a Java program. The program needs its several variables
filled with values before it can be run. This can be achieved by
hard-coded in the program. Alternatively and being nicer and easier, a
script can be used and set the Java program's variables' value and run
the Java program. In this way, if next time, I need to change the value
of the variables, I don't need to change the Java program. I only need
to edit the script.

I have no idea how to do this. I am using Eclipse and running on Windows
XP. Is the script approach only possible on linux OS?

Advertisements

1. The script can use -Dproperty=value for setting the properties,
which you can access in your code using System.getProperty().

2. Store the properties in properties file with name=value format, and
read it in your code using Properties.load(), passing in the
inputstream to the properties file. The script can pass the
path_to_the_properties file as an argument to the program.

Both approaches will work regardless of the underlying Operating
System/IDE.

Advertisements

Re: How to write a script to run a Java program and set variablevalues?

Shawn wrote:
> Hi,
>
> I have written a Java program. The program needs its several variables
> filled with values before it can be run. This can be achieved by
> hard-coded in the program. Alternatively and being nicer and easier, a
> script can be used and set the Java program's variables' value and run
> the Java program. In this way, if next time, I need to change the value
> of the variables, I don't need to change the Java program. I only need
> to edit the script.
>
> I have no idea how to do this. I am using Eclipse and running on Windows
> XP. Is the script approach only possible on linux OS?

A native script in Windows is just a .bat (batch file). I say native
since it is possible to install Perl or tcl/tk or any number of other
languages and run the script that way, although for this type of thing
..bat would be the easiest.

Hi Shawn,
> I have written a Java program. The program needs its several variables
> filled with values before it can be run. This can be achieved by
> hard-coded in the program. Alternatively and being nicer and easier, a
> script can be used and set the Java program's variables' value and run
> the Java program. In this way, if next time, I need to change the value
> of the variables, I don't need to change the Java program. I only need
> to edit the script.

You can write a script that launches your Java application and passes
into it values that your application then uses. A rough example is
outlined below.

The script could be a .sh script on Linux/Unix or a .bat script on
Windows, although these are not the only options.

java MyApp value1 value2

The application then has a main method that accepts value1 and value2
as arguments through the standard "main method", as per below. You
could use those values in the main method by passing them to an
instance of your app or use them directly..

// Use the args directly..
System.out.println("Now using " + firstArg + " and "
+ secondArg + " to set up my app.");

// You can also pass them to a new object for
// use by the app in that way.
MyApp myApp = new MyApp(firstArg, secondArg);
}

// You might have instance variables that need to
// be set from the script.
private String setting1;
private String setting2;

// The constructor then sets up those variables
// from values pass to it from the main method,
// which were passed to it from a script.
public MyApp(String setting1, String setting2) {
this.setting1 = setting1;
this.setting2 = setting2;
}

}

> I have no idea how to do this. I am using Eclipse and running on Windows
> XP. Is the script approach only possible on linux OS? Is the
> script approach only possible on linux OS?

I don't know of an OS (especially Linux/Unix/Windows/Mac) that doesn't
support scripts that can run Java.. so you are in luck!

Also, if you are using Eclipse, you can use "Run | Run .. | Java
Application" to set arguments to the application as well, which is a
handy alternative to a script while you are still writing the code.

Re: How to write a script to run a Java program and set variablevalues?

Shawn wrote:
> I have written a Java program. The program needs its several variables
> filled with values before it can be run.

Provide a configuration file, e.g. located relative to the user.dir (a
system property). Reading (and writing) a configuration file in Java is
rather trivial, since Java comes with an own API for this, the
Properties class.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!