p.waitFor()不返回

时间:2018-09-18 21:24:39

标签: java processbuilder javap

我有以下程序可以在一个简单的文件上运行,但是在Car.class上运行时会挂在p.waitFor()上

更新:当我删除p.waitFor()行时;该程序在更复杂的Car.class文件上运行。那么,为什么将其挂起,我需要将其保留在代码中?

这是程序:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Javap2 {

public static void main(String[] args) {
  try{
   List<String> cmdList = new ArrayList<String>();
   cmdList.add("javap.exe");
   cmdList.add("-c");
   cmdList.add("E:\\JAVA\\BlueJ\\PROJECTS\\cars\\DocFooter.class");
   System.out.println(0);
   // Constructing ProcessBuilder with List as argument
   ProcessBuilder pb = new ProcessBuilder(cmdList);
   System.out.println(1);
   Process p = pb.start();
   System.out.println(2);
   p.waitFor();
   System.out.println(3);
   InputStream fis = p.getInputStream();
   System.out.println(4);
   DisplayClassStructure(fis);
  }catch(Exception e){
 System.out.println(e);
  }
}

 // Method used for displaying the disassembled class
 private static void DisplayClassStructure(InputStream is) throws IOException{

   InputStream stream;  

   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   String line;
   List<String> commands = new ArrayList<String>();
   String[] ifs = {": if_",": ifeq",": ifge",": ifgt",": ifle",": iflt",": ifne",": ifnonnull",": ifnull"};

   int i =0;
   while ((line = reader.readLine()) != null) { 
        commands.add(line.trim());
        i++;           
   }
   // Better put it in finally
   reader.close();
   for (int j = 0; j < i; j++){           
      System.out.println(j + "  " + commands.get(j));

   }
 }
}

这是适用于DocFooter.java的简单案例:

public class DocFooter  {
   public void init()
   {
      int x = 5;
      if(5 == x){
        System.out.println("yes we are");
      }
   }
}

这是Car.java,当被Javap.class的主程序调用时,它不起作用:

import java.util.Random;

public class Car
{
    /* This section declares our instance variables for the class */ 
    private String make;
    private int year;
    private double mileage; 
    private int lastTireChange;
    private String carColor;

    /**
     * Constructor for objects of class Car
     */
    public Car()
    {
        // initialise instance variables
        make           = "defaultCar";
        year           = 1904;
        mileage        = 0.0;
        carColor       = "black";
        lastTireChange = (int) mileage;

    }

    public Car(String updateMake, int updateYear, double updateMileage, String updateCarColor)
    {
        // initialise instance variables
        make           = updateMake;
        year           = updateYear;
        mileage        = updateMileage;
        carColor       = updateCarColor;
        lastTireChange = (int) mileage;
    }

    public static void main(String[] args)
    {
        System.out.print('\u000C');

        Car mine = new Car("BMzW",2018,60000.0,"blue"); //declareing a new Car
        Car yours = new Car();                          //declareing a new Car

        System.out.println("Print out initial instance variables from both constructors");
        System.out.println("BMzW,2018,60000.0,blue,60000");
        System.out.println("default car,1904,0.0,black,0");
        mine.printVars(mine);
        yours.printVars(yours);

        System.out.println("Print out values after each method has been called");
        mine.callMethods(mine);

        mine.oldCarNewColor();
    }

    public String getMake()
    {
        return make;
    }

    public void setMake(String updateMake)
    {
        make = updateMake;
    }

    public int getYear()
    {
        return year;
    }

    public void setYear(int updateYear)
    {
        year = updateYear;
    }

    public double getMileage()
    {
        return mileage;
    }

    public void setMileage(double updateMileage)
    {
        mileage = updateMileage;
    }

    public String getCarColor()
    {
       return carColor;
    }

    public void setCarColor(String updateCarColor)
    {
        carColor = updateCarColor;
    }
    public int getLastTireChange()
    {
         return lastTireChange;
    }
    public void setLastTireChange(int updateLastTireChange)
    {
         lastTireChange = updateLastTireChange;
    }

    public boolean tireChange()
    {
        if (mileage - (double)lastTireChange > 40000.){            
        setLastTireChange((int) getMileage());
        return true;
        }else return false;
    }

    public void oldCarNewColor()    {

        String[] colors = {"blue",
                           "black",
                           "brown",
                           "red",
                           "green",
                           "grey",
                           "white",
                           "yellow",
                           };
        int size        = colors.length;        // local var array length

        Random random   = new Random();         // imported Random class now declaring a new instance
        int index       = random.nextInt(size); //local var index for use in String array

        if (mileage > 50000) carColor = colors[index]; 
    }
    public void printVars(Car sample)
    {
       System.out.println("");
       System.out.println("MAKE       : " + sample.getMake());
       System.out.println("YEAR       : " + sample.getYear());
       System.out.println("MILEAGE    : " + sample.getMileage());
       System.out.println("CAR COLOR  : " + sample.getCarColor());
       System.out.println("TIRE MILES : " + sample.getLastTireChange()); 
       System.out.println("");
    }

    public void callMethods(Car sample){
        System.out.println("");
        sample.setMake("mercedes");
        System.out.println("MAKE updated       to (mercedes) : " + sample.getMake() );
        sample.setYear(2000);
        System.out.println("YEAR updated       to (2000)     : " + sample.getYear());
        sample.setMileage(75000.0);
        System.out.println("MILEAGE updated    to (75000.0)  : " + sample.getMileage());
        sample.setCarColor("red");
        System.out.println("CAR COLOR updated  to (red)      : " + sample.getCarColor());
        sample.setLastTireChange(15000);
        System.out.println("TIRE MILES updated to (15000)    : " + sample.getLastTireChange());
        System.out.println();
        System.out.println("Call method tireChange - expect true");        
        System.out.println(sample.tireChange()); 
        System.out.println("TIRE MILES updated to (75000)    :  " + sample.getLastTireChange());
        System.out.println();
        System.out.println("Call method tireChange again - expect false");

        System.out.println(sample.tireChange()); 
        System.out.println("TIRE MILES updated to (no change): " + sample.getLastTireChange());

        System.out.println();
        sample.oldCarNewColor();

        System.out.println("CAR COLOR updated to (random color): " + sample.getCarColor());
    }

}

0 个答案:

没有答案