So the title pretty much covers it.
I finally got my program to make a .java and .class file and execute other runnable .jar files.
I also finally got my program to output those .java and .class files as a .jar file. Now though when I try and run the runnable .jar file nothing happens.
So please as I'm so close to finish this part of my project to move onto the next part point out where my code has gone wrong.
I think it is this line that is rather huge:
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");
Here is the code:
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();
}
}
}
}
One thing you have to understand is that JVM executes class files which are produced from source files. Hence, I would never expect a coding like the following to execute:
bw.write("public class BasicGUI" + buffer + "{" + NL
+ "\tpublic static void main(String[] args) {" + NL
+ "\t\tSystem.out.println(\"hello world\");" + NL
+ "\t}" + NL
+ "}" + NL);
Check the jar command usage and try to create the jar in a properway