1

I have created A GUI using java and netbeans IDE, in that java code i have set value of a variable path which consists of path of directory such as path=/home/usr/jai/newfolder and i have a shell script file with extension .sh in that a command is mentioned which access many files mentioned in newfolder.

My question is how to use path variable in that shell script file so that I can access all those files mention in newfolder and execute that file using java code and I know how to execute script file using java code but I am unable to access the variable value in shell script file.

5
  • 2
    Pass the value as a parameter to the shell script when you call it. Commented Jul 4, 2014 at 7:18
  • 1
    ...and access it in your script using $<n> where n is the position where the variable is located in arguments of the script. Commented Jul 4, 2014 at 7:23
  • String cmnd1=path+"/test_script_gui.sh"; System.out.println(cmnd1); String cmnd2[]={cmnd1,path}; try { Runtime.getRuntime().exec(cmnd2); } catch (IOException ex) { Logger.getLogger(filedata.class.getName()).log(Level.SEVERE, null, ex); } Commented Jul 4, 2014 at 9:07
  • Following is the command given where I have to use variable path in the place of dot signifying current directory HVite -T 1 -D -A -H ./work_all_3_train_read_test_aprl_20145/hmm$i/macros -H ./work_all_3_train_read_test_aprl_20145/hmm$i/hmmdefs -S ./test_gui.list -C ./analysis_train.conf -I ./test.mlf -i ./work_all_3_train_read_test_aprl_20145/hmm$i/recout_test_gui.mlf -o SWT -w path/wdnet.txt -p -10.0 -s 0 path/dict.txt ./hmmlist.txt . How to do this?? Commented Jul 4, 2014 at 9:14
  • This command is used in script file where variable path is to be used. Is the above right for passing the argument path to script file? How to use this argument in script file Commented Jul 4, 2014 at 9:17

1 Answer 1

1

You can get the value of environment variable in Java like:

Ref:

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

Then pass that variable value as a command line argument to that shell script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.