سؤال

لقد قمت بإنشاء فصل دراسي لتجميع ملفات .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.MarketWatchBean الرجاء إزالة أو تأكد من ظهوره في الدليل الفرعي الصحيح لل 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();
            }
        }
هل كانت مفيدة؟

المحلول

عليك تمرير الخيار -d dest_directory إلى الطريقة compiler.run(null,null,null,"-d destdirectory",fileToCompile); الرجاء التأكد من أن الدليل الوجهة موجود بالفعل.

من توثيق,

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

يمكنك تمرير عدد متغير من الوسائط إلى الأداة.على سبيل المثال، يجب تمرير خيارات أداة javac كوسائط لهذه الطريقة

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top