質問

.javaファイルを手動でコンパイルするためのクラスを.classファイルに作成しました。このプログラムは正常に機能します。ただし、.classファイルは、.javaファイルと同じディレクトリに作成されます。しかし、私はそれらをいくつかのカスタムディレクトリで作成したいです。私は何ができますか?

以下は、.javaファイルをコンパイルするために使用しているコードです。: -

// * ** * ** * ** > * ** * ** * ** * ** //

//これは.javaファイルを.classファイルにコンパイルし、それを同じ場所

に保存します。
public void compileFile(String pageName,String packageName) {

    String fileToCompile = packageName  + pageName +".java";

    System.out.println("String to compile :- " + fileToCompile );

    System.setProperty("java.home", "C:\\install\\Java\\jdk1.7");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    int compilationResult = compiler.run(null, null, null, fileToCompile);

            if(compilationResult == 0){

                System.out.println("Compilation is successful");

            }else{

                System.out.println("Compilation Failed");
              //  this.deleteFiles(fileToCompile);

            }


    } 
.

//このメソッドは、(コピーして貼り付け)マイ作成された.Classファイルをカスタムディレクトリに移動します。 しかし、それは悪いクラスファイルのようないくつかのエラーを与えます:MyCustomDir \ MarketwatchBean.class クラスファイルには、間違ったクラスが含まれています.MyCustomDir.CormetWatchBean クラスパスの正しいサブディレクトリに表示されていることを確認してください。

public void moveFiles(String sourcePath, String destPath){

        InputStream inStream = null;
        OutputStream outStream = null;

            try{

                File afile =new File(sourcePath);
                File bfile =new File(destPath);

                inStream = new FileInputStream(afile);
                outStream = new FileOutputStream(bfile);

                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = inStream.read(buffer)) > 0){

                    outStream.write(buffer, 0, length);

                }

                inStream.close();
                outStream.close();

                //delete the original file
               // afile.delete();

                System.out.println("File is copied successfully!");

            }catch(IOException e){
            //  this.deleteFiles(sourcePath);
            //  this.deleteFiles(destPath);
                e.printStackTrace();
            }
        }
.

役に立ちましたか?

解決

オプション-d dest_directoryをメソッドcompiler.run(null,null,null,"-d destdirectory",fileToCompile);に渡す必要があります。宛先ディレクトリがすでに存在することを確認してください。

ドキュメント

int run(InputStream in,
        OutputStream out,
        OutputStream err,
        String... arguments)
.

ツールに変数数の引数を渡すことができます。I.E Javacツールオプションは、このメソッド

の引数として渡す必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top