Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2001-09-18
7
483 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 47
ejb stateless example 2 18
How to convert from xls to xlsx using java 7 54
SequenceInputStream example 3 19
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
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 …

790 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