我正在从类batchrun调用类guibuilder。 Batchrun的代码是:-

public class batchrun {
public static String md5gen(String a) throws NoSuchAlgorithmException
{
    MessageDigest m= MessageDigest.getInstance("MD5");
    m.reset();
    m.update(a.getBytes());
    byte[] digest=m.digest();
    BigInteger bigInt = new BigInteger(1,digest);
    String hashtext = bigInt.toString(16);
    while(hashtext.length() < 32 ){
      hashtext = "0"+hashtext;
    }
    return hashtext;
}
private static String getInputAsString(InputStream is)
{
   try(java.util.Scanner s = new java.util.Scanner(is))
   {
       return s.useDelimiter("\\A").hasNext() ? s.next() : "";
   }
}
public static void main(String[] args) throws InterruptedException {
    try {

        guibuilder.main(args);
        guibuilder gb=new guibuilder();
        String fg=guibuilder.antd;
        String arg1=gb.arg;
        String userinp1=gb.userinp;

        System.out.println("FG="+fg+" arg1="+arg1+" userinp="+userinp1);


  Process pan =  Runtime.getRuntime().exec(new String[]    {"C:\\test1.bat",arg1,fg});

        pan.waitFor();



            String extra="\\";
            extra+=userinp1;
            String patha=fg+extra;
            ProcessBuilder pb = new     ProcessBuilder("adb","shell","getprop","ro.csc.sales_code");
            Process p=pb.start();
            p.waitFor();
            String stdout = getInputAsString(p.getInputStream());
            String newstring=stdout.substring(0,3);;
            String fn=fg+"\\"+newstring+".txt";
            ZipFile zipFile = new ZipFile(patha);
            Enumeration<?> enu = zipFile.entries();
            int flag=0;
            String so="so";
            File file = new File(fn);
        FileOutputStream fos = new FileOutputStream(file);
            PrintStream ps = new PrintStream(fos);

            System.setOut(ps);

            while (enu.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) enu.nextElement();
                String name = zipEntry.getName();
                long size= zipEntry.getSize();
                String extension= name.substring(name.lastIndexOf(".")+1,name.length());

                if(extension.equals(so))
                {
                    String plaintext=name+size;
                    String md5result=md5gen(plaintext);
                    System.out.println(name+"   "+size+"   "+md5result);
                    ++flag;

                }

            }
            if(flag==0)
                System.out.println("fail");


}catch (IOException ex){
    System.out.println(ex.getMessage());
} catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    }

}


guibuilder的代码是

public class guibuilder {

private JFrame frame;
public static String antd;
public static String arg;
public static String userinp;


/**
 * Launch the application.
 * @return
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                guibuilder window = new guibuilder();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

/**
 * Create the application.
 */
public guibuilder() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnExtract = new JButton("Extract");
    btnExtract.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            fchooser fc1=new fchooser();
            antd=fc1.demo();
             arg=JOptionPane.showInputDialog("Enter the apk path");
             userinp=JOptionPane.showInputDialog("Enter the apk name");



        }
    });
    btnExtract.setBounds(69, 55, 89, 23);
    frame.getContentPane().add(btnExtract);

    JButton btnCompare = new JButton("Compare");
    btnCompare.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.dispose();
            newjframe n = new newjframe();
            n.setVisible(true);
        }
    });
    btnCompare.setBounds(261, 55, 89, 23);
    frame.getContentPane().add(btnCompare);
}
}


我希望程序在继续批处理代码之前,先等待guibuilder的执行。但是在这段代码中,我什至没有选择guibuilder中的文件,程序继续执行,并且System.out.println(“ FG =” + fg +“ arg1 =” + arg1 +“ userinp =” + userinp1);在我在guibuilder中选择任何内容之前,将打印此行。

最佳答案

您的代码表明您的Java项目有两个主要的类。一个在batchrun类中,另一个在guibuilder类中。 [摘自您的声明guibuilder.main(args)]

在项目中仅使用一个主类。这可能会解决您的问题。

我认为您的guibuilder类具有这样的结构

class guibuilder{
    ...... //global variables

    public static void main(String[] args){
        ......... //statments

    }
}


不要在单个项目中使用两种主要方法。

您需要像这样构造guibuilder类(如下所示)

class guibuilder{
    ...... //global variables

    public static void buildGui(){//you can use any method name here
        ......... //statments

    }
}


从另一个类调用此方法,只需使用此语句

guibuilder.buildGui()


或者,另一种方式

class guibuilder{
    ...... //global variables

    public void buildGui(){//you can use any method name here
        ......... //statments

    }
}


从另一个类调用此方法使用此语句

guibuilder gui=new guibuilder();
gui.buildGui();

07-24 16:18