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

S

Shawn

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?

Thank you very much.
 
M

Manish Pandit

Two options:

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.

-cheers,
Manish
 
B

Brandon McCombs

Shawn said:
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.
 
R

Robert Mark Bram

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..

public class MyApp {

public static void main (String [] args) {
String firstArg = args[0];
String secondArg = args[1];

// 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.

Rob
:)
 
T

Thomas Weidenfeller

Shawn said:
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.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,099
Members
48,773
Latest member
Kaybee

Latest Threads

Top