Solved

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

Posted on 2001-09-18
7
478 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 65
Java Server Faces parameter pass? 6 37
how do i compare an object based on two fields 6 48
varialbe initialization 11 30
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now