0

所以标题几乎涵盖了它。

我终于让我的程序制作了一个 .java 和 .class 文件并执行其他可运行的 .jar 文件。

我也终于让我的程序将这些 .java 和 .class 文件输出为 .jar 文件。现在,当我尝试运行可运行的 .jar 文件时,什么也没有发生。

因此,当我快要完成项目的这一部分时,请指出我的代码出了问题的下一部分。

我认为这条线相当大:

 ProcessBuilder javaCompiler = new ProcessBuilder("jar", "-cvfm", "BasicGUI" + buffer + ".jar", "C:/Users/Powermaniac/workspace/GUI program/MANIFEST 1." + buffer + extension2, "C:/Users/Powermaniac/workspace/GUI program/bin/test/" + "*.class");

这是代码:

 package test;

import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.*;

public class BasicGUI {
   public static void main(String[] args) throws IOException, InterruptedException {
       {    
           BufferedReader br;
           BufferedWriter bw;
           String fname = "BasicGUI.jar";
           String extension = ".java";
           String extension2 = ".MF";
           String buffer = "1";

           for (int h = 1; h <= 1; h++) {
               buffer = "" + h;
               String fileName = "BasicGUI";
               fileName = fileName + buffer + extension;
               System.out.println(fileName);
               try {
                   bw = new BufferedWriter(new FileWriter(fileName));
               } 
               catch (IOException e) {
                   System.out.println("Cannot open " + fileName + "!");
                   return;
               }
               try{
                   final String NL = System.getProperty("line.separator");
                   bw.write("public class BasicGUI" + buffer + "{" + NL
                           + "\tpublic static void main(String[] args) {" + NL
                           + "\t\tSystem.out.println(\"hello world\");" + NL
                           + "\t}" + NL
                           + "}" + NL);
                   bw.close();

                   br = new BufferedReader(new FileReader(fileName));
               }
               catch (FileNotFoundException e) {
                   System.out.println(fname + " not found!");
                   return;
               }

               String line;
               while( (line = br.readLine()) != null) {
                   System.out.println(line);
               }
           }


           for (int h = 1; h <= 1; h++) {
               buffer = "" + h;
               String fileName = "MANIFEST 1.";
               fileName = fileName + buffer + extension2;
               System.out.println(fileName);
               try {
                   bw = new BufferedWriter(new FileWriter(fileName));
               } 
               catch (IOException e) {
                   System.out.println("Cannot open " + fileName + "!");
                   return;
               }
               try{
                   final String NL = System.getProperty("line.separator");
                   bw.write("Manifest-Version: 1." + buffer + NL
                           + "Created-By: 1.6.0 (Sun Microsystems Inc.)" + NL
                           + "Class-Path: C:/Users/Powermaniac/workspace/GUI program/bin/test" + NL
                           + "MainClass: BasicGUI" + buffer + NL);
                   bw.close();

                   br = new BufferedReader(new FileReader(fileName));
               }
               catch (FileNotFoundException e) {
                   System.out.println(fname + " not found!");
                   return;
               }

               String line;
               while( (line = br.readLine()) != null) {
                   System.out.println(line);
               }
           }

           HelloWorldDisplay displayPanel = new HelloWorldDisplay();
           JButton okButton = new JButton("OK");
           ButtonHandler listener = new ButtonHandler();
           okButton.addActionListener(listener);

           JPanel content = new JPanel();
           content.setLayout(new BorderLayout());
           content.add(displayPanel, BorderLayout.CENTER);
           content.add(okButton, BorderLayout.SOUTH);

           JFrame window = new JFrame("GUI Test");
           window.setContentPane(content);
           window.setSize(250,100);
           window.setLocation(100,100);
           window.setVisible(true);

           ProcessBuilder javaCompiler = new ProcessBuilder("jar", "-cvfm", "BasicGUI" + buffer + ".jar", "C:/Users/Powermaniac/workspace/GUI program/MANIFEST 1." + buffer + extension2, "C:/Users/Powermaniac/workspace/GUI program/bin/test/" + "*.class");

           javaCompiler.redirectErrorStream(true);
           Process p = javaCompiler.start();
           p.waitFor();

           InputStream inp=p.getInputStream();
           int no=inp.read();
           while(no!=-1)
           {
            System.out.print((char)no);
            no=inp.read();
           } 

            }   
        }
    }
4

2 回答 2

0

您必须了解的一件事是 JVM 执行从源文件生成的类文件。因此,我永远不会期望执行如下代码:

bw.write("public class BasicGUI" + buffer + "{" + NL
                           + "\tpublic static void main(String[] args) {" + NL
                           + "\t\tSystem.out.println(\"hello world\");" + NL
                           + "\t}" + NL
                           + "}" + NL);

检查jar 命令用法并尝试以正确的方式创建 jar

于 2013-06-08T06:29:57.273 回答
0

我基本上是在尝试制作一个制作程序的程序。

那么在这种情况下,您最好生成一个 Antbuild.xml文件,然后将 Ant 作为外部进程运行,或者在当前 JVM 中调用它。Ant 知道如何运行 Java 编译器和创建 JAR 文件。

这是一个关于如何做到这一点的问答:从 Java 运行 ant

于 2013-06-08T07:26:54.013 回答