Solved

Setting Environment variables and then running a perl script from a java program.

Posted on 2001-09-18
7
482 Views
Last Modified: 2008-02-01
I am trying to run perl script called mib2c for UCD-SNMP on Linux. To automate the process , I want to have a java program that does the following:

1.   Sets an environment variable i.e export MIBS=ALL

2. Runs the perl script with the above settings i.e /root/snmp/ucd-snmp-4.1.2/local/mib2c <options>.

I thought that I could do the above using Runtime class like :

Runtime myRuntime = Runtime.getRuntime();
Process myProcess = myRuntime.exec("export MIBS=ALL");
and then use the output stream of the "myProcess" to pass the command for running the perl script.

However , I realised that this is not possible as I get the following message when I read the error streams of the process:

export: local server: Call timed out
export: MIBS=ALL: No such file or directory

Another approach could be to  to open a socket on port 23(i.e telnet session) and then give login and password and then pass the commands. However , I want this to be my last resort as this would require password etc. which would decrease the security + the code would be bulky.

Can anyone provide a solution for my problem?
0
Comment
Question by:Siddhartha_s
7 Comments
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 6490753
I think you can settthe "export MIBS=ALL" in a batchfile that sets that environment variable.



something like:

(SetAll.Bat)
set MIBS=ALL


then use (erm!, i think you would need some kind of batchfile parser here, maybe command.com?)

Process myProcess=myRuntime.exec("SetAll.Bat");


Erm, this could help with the enviroment variable. But it's early and I could be way off here.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6490754
There are several variants of the exec method that take String[] or environment variable settings in the form "name=value" that will be supplied to the new process.
    http://java.sun.com/j2se/1.3/docs/api/java/lang/Runtime.html

Jim
0
 

Expert Comment

by:ManojShetty
ID: 6491125

To set the environment variable
 System.setProperty(key, Value)

To access it use System.getProperty(key);
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
ID: 6491441
Here are a couple classes that might help you. The first, Environment, gets the OS environment. It doesn't use JNI but it does assume the command env exists. This is not completely platform-independent. The second, Launch, calls the Runtime.exec method I alluded to earlier. It instantiates an Environment object and adds to it any name=value pairs that are found in the command line arguments. The last command-line argument to Launch should be the command that you want to execute.

Best regards,
Jim Cakalic

---------- Environment.java ----------
import java.io.*;
import java.util.*;

public class Environment {
    private Properties _props;

    public Environment() {
        _props = new Properties();
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("env");
            InputStream in = proc.getInputStream();
            _props.load(in);
            in.close();
            proc.destroy();
        } catch (Exception e) {
        }
    }
    public String get(String key) {
        return _props.getProperty(key);
    }
    public String get(String key, String fallback) {
        return _props.getProperty(key, fallback);
    }
    public void set(String key, String val) {
        _props.setProperty(key, val);
    }
    public String[] get() {
        String[] values = new String[_props.size()];
        Enumeration each = _props.keys();
        for (int i = 0; each.hasMoreElements(); ++i) {
            String key = (String)each.nextElement();
            String value = _props.getProperty(key);
            values[i] = key + "=" + value;
        }
        return values;
    }

    public static void main(String[] args) throws Exception {
        Environment env = new Environment();
        String[] values = env.get();
        for (int i = 0; i < values.length; ++i) {
            System.out.println(values[i]);
        }
    }
}
---------- end ----------

---------- Launch.java ----------
import java.io.*;

public class Launch {
    public static void main(String[] args) throws Exception {
        Environment env = new Environment();
        for (int i = 0; i < args.length - 1; ++i) {
            String arg = args[i];
            int index;
            if ((index = args[i].indexOf("=")) >= 0) {
                String key = arg.substring(0, index).trim();
                String value = arg.substring(index + 1).trim();
                env.set(key, value);
            }
        }
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(args[args.length - 1], env.get());
        InputStream in = proc.getInputStream();
        while (in.read() >= 0) {
        }
        proc.waitFor();
    }
}
---------- end ----------
0
 
LVL 20

Expert Comment

by:Venabili
ID: 8932736
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for jim_cakalic

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 8934086
OK. Thanks. :-)
Jim
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question