简体   繁体   English

从另一个班级上课

[英]calling class from another class

I am calling class guibuilder from my class batchrun. 我正在从类batchrun调用类guibuilder。 code for batchrun is :- 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();
}
    }

}

The code for guibuilder is 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);
}
}

I want the program to wait for the execution of guibuilder before continuing with the code from batchrun. 我希望程序在继续批处理代码之前,先等待guibuilder的执行。 But in this code, i have not even selected the files in guibuilder and the program continues execution and System.out.println("FG="+fg+" arg1="+arg1+" userinp="+userinp1); 但是在这段代码中,我什至没有选择guibuilder中的文件,程序继续执行,并且System.out.println(“ FG =” + fg +“ arg1 =” + arg1 +“ userinp =” + userinp1); this line is printed before i choose anything in guibuilder. 在我在guibuilder中选择任何内容之前,将打印此行。

Your code shows that your java project has two main classes. 您的代码表明您的Java项目有两个主要的类。 one in batchrun class another is in guibuilder class. 一个在batchrun类中,另一个在guibuilder类中。 [from your statement guibuilder.main(args) ] [来自您的声明guibuilder.main(args) ]

Use only one main class in your project. 在项目中仅使用一个主类。 This may fix your problem. 这可能会解决您的问题。

I think your guibuilder class has structure like this 我认为您的guibuilder类具有这样的结构

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

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

    }
}  

Don't use two main method in a single project. 不要在单个项目中使用两种主要方法。

you need to structure your guibuilder class like this (shown below) 您需要像这样构造您的guibuilder类(如下所示)

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

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

    }
} 

to invoke this method from another class just use this statement 从另一个类调用此方法,只需使用此语句

guibuilder.buildGui()

or, another way 或者,另一种方式

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

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

    }
} 

to invoke this method from another class use this statement 从另一个类调用此方法使用此语句

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM