Beispiel #1
0
  public Class loadNewClass(String javaFile, String packageName) {
    Class myClass = null;

    String className = javaFile.replace(".java", ".class");

    File inpFile = new File(className);
    int idx = className.lastIndexOf(File.separatorChar);
    String javaName = className.substring(idx + 1, className.indexOf("."));
    int size = (int) inpFile.length();
    byte[] ba = new byte[size];
    try {
      FileInputStream fis = new FileInputStream(className);

      // read the entry
      int bytes_read = 0;
      while (bytes_read != size) {
        int r = fis.read(ba, bytes_read, size - bytes_read);
        if (r < 0) break;
        bytes_read += r;
      }
      if (bytes_read != size) throw new IOException("cannot read entry");
    } catch (FileNotFoundException fnfExc) {
      System.out.println("File : " + className + " not found");
      fnfExc.printStackTrace();
    } catch (IOException ioExc) {
      System.out.println("IO Exception in JavaCompile trying to read class: ");
      ioExc.printStackTrace();
    }

    try {
      String packageAppendedFileName = "";
      if (packageName.isEmpty()) packageAppendedFileName = javaName;
      else packageAppendedFileName = packageName + "." + javaName;
      packageAppendedFileName.replace(File.separatorChar, '.');
      myClass = defineClass(packageAppendedFileName, ba, 0, size);
      // SOS-StergAutoCompletionScalaSci.upDateAutoCompletion(myClass);
      String userClassName = myClass.getName();
      JOptionPane.showMessageDialog(null, "Class " + userClassName + " loaded successfully !");
    } catch (ClassFormatError exc) {
      System.out.println("error defining class " + inpFile);
      exc.printStackTrace();
    } catch (Exception ex) {
      System.out.println("some error defining class " + inpFile);
      ex.printStackTrace();
    }
    return myClass;
  }
Beispiel #2
0
  // compile a Java file with the installed javac compiler
  public boolean compileFile(String sourceFile) {
    boolean compilationResult = true; // no errors

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) { // Sterg-SOS why not loaded?
      System.out.println(
          "ToolProvider.getSystemJavaCompiler: no compiler provided. Unable to compile");
    }
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

    StringWriter compileWriter = new StringWriter();

    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(new File(sourceFile));
    Iterable<? extends JavaFileObject> compilationUnits =
        fileManager.getJavaFileObjectsFromFiles(sourceFileList);

    String pathOfFile = sourceFile.substring(0, sourceFile.lastIndexOf(File.separatorChar));
    String classpath =
        GlobalValues.jarFilePath
            + File.pathSeparatorChar
            + pathOfFile
            + File.pathSeparatorChar
            + ".";
    Iterable<String> options = Arrays.asList("-cp", classpath);

    CompilationTask task =
        compiler.getTask(compileWriter, fileManager, null, options, null, compilationUnits);

    boolean compileResult = task.call();
    if (compileResult == false) {
      compilationResult = false; // compilation errors
      JFrame compResultsFrame = new JFrame("Compilation Results");
      String diagnString = compileWriter.toString();
      JTextArea compResultsArea = new JTextArea(diagnString);
      compResultsArea.setFont(new Font("Arial", Font.BOLD, 16));
      JPanel compResultsPanel = new JPanel();
      compResultsPanel.add(compResultsArea);
      compResultsFrame.add(compResultsPanel);
      compResultsFrame.setSize(800, 200);
      compResultsFrame.setLocation(100, 200);
      compResultsFrame.setVisible(true);
      int response =
          JOptionPane.showOptionDialog(
              null,
              "File " + sourceFile + " has compilation errors ",
              "Edit File? ",
              JOptionPane.YES_NO_OPTION,
              JOptionPane.QUESTION_MESSAGE,
              null,
              null,
              null);
      if (response == JOptionPane.YES_OPTION) { // edit it with scalalabEditor
        new scalalabEdit.EditorPaneEdit(sourceFile);
      }
    }

    try {
      fileManager.close();
    } catch (IOException e) {
    }

    return compilationResult;
  }