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

Discussion in 'Java' started by Shawn, Nov 1, 2006.

  1. Shawn

    Shawn Guest

    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.
     
    Shawn, Nov 1, 2006
    #1
    1. Advertising

  2. 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
     
    Manish Pandit, Nov 1, 2006
    #2
    1. Advertising

  3. 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.
     
    Brandon McCombs, Nov 1, 2006
    #3
  4. 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
    :)
     
    Robert Mark Bram, Nov 2, 2006
    #4
  5. 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.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
     
    Thomas Weidenfeller, Nov 2, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    4
    Views:
    739
  2. Shawn
    Replies:
    2
    Views:
    1,485
    Shawn
    Dec 6, 2006
  3. BillJosephson
    Replies:
    7
    Views:
    380
    John W. Kennedy
    Jan 24, 2007
  4. Replies:
    9
    Views:
    982
    Paddy O'Loughlin
    Feb 27, 2009
  5. sahm
    Replies:
    4
    Views:
    687
Loading...

Share This Page