-1

Getting insight from this post, I need to have a java program which should run my other program (a C++ program) with some arguments, in Ubuntu, if it is not already running (I need to check if it is already running using ps -A | grep 'test' to see if the result is null). The command to run my desired program looks like this: ./src/test -w -b -f tracefiles/trace111.txt -z. How can I do this, no matter what directory my java program is currently in? Unfortunately my java program shows this error: No such file or directory while if I run the same command from terminal from the same directory of my Java class file, it runs it.

Here is my simple code:

import java.io.*;

public class JavaRunCommand {

    public static void main(String args[]) {

        String s = "./../../../Downloads/yse.wzt/src/yse4 -w -b -f ../../../Downloads/yse.wzt/tracefiles/capacity.10MbpsUP_1MbpsDown_400RTT_PER_0.0001.txt -z -at=eth0 -an=eth1";

        try {

            // run the Unix "ps -ef" command
            // using the Runtime exec method:
            Process p = Runtime.getRuntime().exec(s);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(
                    p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command
            System.out
                    .println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

            System.exit(0);
        } catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
    }
}
Community
  • 1
  • 1
Tina J
  • 4,983
  • 13
  • 59
  • 125

1 Answers1

0

You need to add the JAVA bin direction to your PATH variable. Edit the .bashrc in your home directory and add the following line:

export PATH=/path/to/java/bin:$PATH

You'll need to 'source' your .bashrc file or restart your terminal for the path to take effect. In your home directory type:

source .bashrc

More information on setting environment variables on Ubuntu is available here: https://help.ubuntu.com/community/EnvironmentVariables#Persistent_environment_variables.

You can confirm whether the PATH assignment was correct with echo $PATH or simply typing javac from a directory other than the java/bin directory.

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46