Java - 수동 컴파일 된 클래스 파일의 위치를 설정합니다
-
21-12-2019 - |
문제
iClass 파일에 .java 파일을 수동으로 컴파일하는 클래스를 만들었습니다.이 프로그램은 성공적으로 작동합니다.그러나 .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 파일을 사용자 정의 디렉토리에 복사 및 붙여 넣기로 이동하려고 시도합니다. 그러나 Bad 클래스 파일과 같은 오류가 발생합니다. MyCustomDir \ MarketWatchBean.class 클래스 파일에는 잘못된 클래스가 들어 있습니다. myCustomDir.MarketWatchBean 클래스 경로의 올바른 하위 디렉토리에 나타나지 않도록 제거하거나 확인하십시오.
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
메소드에 전달해야 할 수 있습니다. 대상 디렉토리가 이미 있는지 확인하십시오.
int run(InputStream in,
OutputStream out,
OutputStream err,
String... arguments)
.
가변 수의 인수를 도구에 전달할 수 있습니다.I.E javac 도구 옵션은이 메소드의 인수로 전달되어야합니다
제휴하지 않습니다 StackOverflow