所以标题几乎涵盖了它。
我终于让我的程序制作了一个 .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();
}
}
}
}