I have created a class to manually compile my .java files into .class files. This program works successfully . But, .class files are created in the same directory as my .java files. But, I want them to be created in some custom directory. What can I do?
Below is the code I am using to compile .java files. :-
//***************************//
//this will compile my .java file into .class file and store it in the same location
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);
}
}
//this method tries to move (by copy and paste) my generated .class file into custom directory. but it gives some error like bad class file: mycustomdir\MarketWatchBean.class class file contains wrong class: mycustomdir.MarketWatchBean Please remove or make sure it appears in the correct subdirectory of the classpath.
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();
}
}