0

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();
           } 

            }   
        }
    }
  • 1
    Based on the fact you're calling out to the compiler and creating a jar, it looks to me like maybe you're trying to do build scripting. Don't use Java for that. I believe ant and Maven are the most commonly used build tools for Java, but I could be wrong. If you are trying to automate the build, welcome to the world of automation; you'll never go back once you get going with it. "Continuous integration" might be a term you want to read about. – jpmc26 Jun 08 '13 at 06:34

2 Answers2

0

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

Chris
  • 5,584
  • 9
  • 40
  • 58
  • I think you just made me realise my mistake. I need to have that write the program I already have again but increment the name by 1. Thus I didn't realise before that I was trying to open a .jar that didn't have a GUI which I discovered earlier is the trick to getting a runnable .jar to produce something that signalises that it is working. Would you happen to know of a way to have the program put itself into the next file without re-typing it all in ""? – Powermaniac Jun 08 '13 at 06:36
  • I still don't understand the goal of creating the classes at runtime. Are you talking about Generics ? Whatever your goal is, am pretty sure that it could be done in a cleaner and better way than creating by incrementing class names by one. Is it possible for you to state the real problem statement ? – Chris Jun 08 '13 at 06:50
  • Sure, hopefully this doesn't get locked or something like the last post I made on here. I'm basically trying to make a program that makes programs. That creates a new .java and .class, compiles them, then executes it. Then from there the program that started it closes and the new one takes over. On top of that I want to add an AI eventually that is basically self improving, well that is the long term goal as I don't know any where near enough Java to be able to do it besides tinkering with open source AI I've found. – Powermaniac Jun 08 '13 at 06:54
0

I'm basically trying to make a program that makes programs.

Well in that case you would be better off generating an Ant build.xml file, and then either running Ant as an external process, or calling it within the current JVM. Ant knows all about how to run the Java compiler and create JAR files.

Here's a Q&A on how to do it: Run ant from Java

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Would that mean starting over basically with ANT? As I've made it so far and all I need now is a couple last pieces before I work on making an AI which I assume will be the hardest of all. Those couple last pieces include: Getting the new program to copy the old programs code. Be able to check if the old program is still running if so to close it. And I think that is currently it. – Powermaniac Jun 08 '13 at 07:45