2017-11-30 12 views
-1

私はcmdのjavaプログラムをjava Mainのように実行しています。しかし、その前に、cd ...を使用してルートディレクトリに移動しなければなりません。これは、メインクラスがルートディレクトリにあるプロパティファイルから値を読み込んでいるためです。ルートディレクトリを設定する方法やオプションがあるので、cdコマンドでこのディレクトリにアクセスする必要はありませんか?cmdのjavaコマンドのデフォルトディレクトリの設定方法

+0

[システムコマンド - Java]の可能な複製(https://stackoverflow.com/questions/7294710/system-commands-java) –

+0

@ShivendraAgarwal私はこの質問がこのようなものではないと思います。この人はシステムコマンドを実行しようとしていません - 実際に彼らは反対をしようとしています。 "それでは、私はcdコマンドでこのディレクトリにアクセスする必要はありません。" – csmckelvey

+0

"デフォルトディレクトリ"はありません。 *現在の作業ディレクトリ*があり、cwd'または 'cd'で設定します。 – EJP

答えて

0

ディレクトリにパスをパラメータとして渡し、String[] argsからmainメソッドに入ることができます。あなたはファイルへの絶対パスを渡しますが、それはあなたがあなたのJavaプロセスを開始しているディレクトリからは関係ありません。

これはどのように行うことができるかを示すoracle's tutorialです。コマンドプロンプトを通じてディレクトリにナビゲート

+0

cmdで 'java'コマンドを呼び出すときに、どのようにパラメータを渡すことができますか? –

+0

答えに例をあげてリンクを追加しました。 –

0

whileループといくつかの単純な文字列処理を使用して非常に簡単です:

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"); 
0

あなたは、Javaの-cp(クラスパス)のコマンドライン引数を使用することができます。その後

ディレクトリ構造はあなただけ<application folder>に移動し、アプリケーションを起動するためにjava -cp bin Mainを使用することができ

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

ある場合。現在のフォルダ内のファイルとしてconfig.propsを参照することもできます(現在のフォルダにあるため)。

これは常に入力したくないものであるため、config.propsの横にあるWindows .batファイルまたは* nix .shスクリプトでラップできます。

関連する問題