Solved

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

Posted on 2001-09-18
7
493 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
[X]
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
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

636 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