-2

I am running java program from cmd like this java Main. But before that, I have to get to the route directory using cd ..., because my Main class is reding values from property file, which is in root directory. Is there a way or an option of setting the root directory, so then I will no need to get to this directory with cd commands ?

Marged
  • 10,577
  • 10
  • 57
  • 99
M. Barabas
  • 53
  • 1
  • 2
  • 11
  • Possible duplicate of [System Commands - Java](https://stackoverflow.com/questions/7294710/system-commands-java) – Shivendra Agarwal Nov 30 '17 at 22:13
  • @ShivendraAgarwal I don't think that question is anything like this one. This person is not trying to execute system commands - in fact they are trying to do the opposite. "_so then I will no need to get to this directory with cd commands_" – takendarkk Nov 30 '17 at 22:19
  • There is no 'default directory'. There is the *current working directory,* and you set it with cwd` or `cd`. – user207421 Nov 30 '17 at 22:58

3 Answers3

0

You could pass path to the directory as a parameter and get in from String[] args in main method. You'll pass absolute path to the file and it wouldn't be relevant from which directory you're starting your java process.

Here is the oracle's tutorial showing how you could do that.

Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
0

Navigating to the directory through command prompt is very easy using a while loop and some simple String processing:

    System.out.println("Please navigate to the desired directory then type 'done'...");

    @SuppressWarnings("resource")
    Scanner scanner = new Scanner(System.in);

    StringBuilder path = new StringBuilder(); //Storage for dir path
    path.append(System.getenv("SystemDrive"));
    while(scanner.hasNextLine()) {

        String command = scanner.nextLine(); //Get input    
        if(command.equalsIgnoreCase("done")) {
            break;
        }else if(command.startsWith("cd")){
            String arg = command.replaceFirst("cd ", ""); //Get next dir
            if(!arg.startsWith(System.getenv("SystemDrive"))) { //Make sure they are not using a direct path
                if(!new File(path.toString() + "/" + arg).exists()) { //Make sure the dir exists
                    arg = arg.equalsIgnoreCase("cd") ? "" : arg;
                    System.out.println("Directory '" + arg + "' cannot be found in path " + path.toString());
                }else {
                    if(arg.equals("..."))
                        path = new StringBuilder(path.substring(0, path.lastIndexOf("/"))); //StringBuilder#substring does not alter the actual builder
                    else
                        path.append("/" + arg);
                    System.out.println("\t" + path.toString()); //Add the dir to the path
                }
            }else { //If they are using a direct path, delete the currently stored path
                path = new StringBuilder();
                path.append(arg);
                System.out.println("\t" + path.toString());
            }
        }else if(command.equalsIgnoreCase("dir")) {
            System.out.println(Arrays.toString(new File(path.toString() + "/").list()));
            //List the dirs in the current path
        }else {
            System.out.println("\t" + command + " is not recognized as an internal command.");
        }
    }

    //Get your file and do whatever
    File theFile = new File(path.toString() + "/myFile.properties");
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
0

You could use the -cp (classpath) command-line argument of Java.

Then if your directory structure is

<application folder>
|
|--config.props  (properties file)
|
\--bin
   |
   |--Main.class
   |
   |... other classes

You could just go to the <application folder> and use java -cp bin Main to start the application. And it could refer to config.props as a file in the current folder (because it is in the current folder).

As this is something what you may not want to type all the time, it could be wrapped in a Windows .bat file or *nix .sh script, residing in , next to config.props.

tevemadar
  • 12,389
  • 3
  • 21
  • 49