java -setting the location of manually compiled class files

231 views Asked by At

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();
            }
        }
1

There are 1 answers

0
Keerthivasan On BEST ANSWER

You have to pass the option -d dest_directory to the method compiler.run(null,null,null,"-d destdirectory",fileToCompile); Please ensure that the destination directory already exists.

From the documentation,

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

You can pass variable number of arguments to the tool. i.e javac tool options should be passed as the arguments to this method