简体   繁体   中英

How can I make a third class that could run these two classes?

How would I be able to run both of these classes in one executable file. I'm new to this so sorry if the answer is very simple. I would appreciate it if you were able to walk me through it. Thank you for taking the time to respond.

import java.util.HashSet;

public class Prog2 {
    public static boolean sameElements(int[] A, int[] B){
        boolean same = false;
        HashSet<Integer> hashSet = new HashSet<Integer>();
        for(int i = 0; i < A.length; i++){
            hashSet.add(A[i]);
        }
        for(int i = 0; i < B.length; i++){
            if(hashSet.contains(B[i])){
                hashSet.remove(B[i]);
            }
        }
        if(hashSet.isEmpty()){
            same = true;
        }
        return same;
    }
}

public class Prog3 {
    public static void inRun(int[] A){
        boolean inRun = false;
        for(int i = 0; i < A.length; i++){
            if(inRun) {
                if (A[i] != A[i - 1]) {
                    System.out.print(')');
                    inRun = false;
                }
            }
            else{
                if(i + 1 < A.length && A[i] == A[i + 1]){
                    System.out.print('(');
                    inRun = true;
                }
            }
            System.out.print(" " + A[i] + " ");
        }
        if(inRun){
            System.out.print(')');
        }
    }
}

Your classes do not have main functions so they cannot be executed. But if they did have main functions:

First, they are both public classes so you need to put each in its own file. (ie Prog2.java and Prog3.java) Also, they would be in the same package so you would add package com.domain.progs; to the top of both files.

Second, you would have to call one class from another. So put this in Prog2's main function:

Prog3.main();

Third, you need to put the whole package in a jar file. Your package structure in the source directory should look like this:

com
|____domain
    |_______progs
           |______Prog2.java
           |______Prog3.java

Compile the files so your out (or bin, etc.) looks like that ^ but with class files instead of java files.

Fourth, create a manifest file that specifies which class to execute. Type this into a file called MANIFEST.MF (make sure to not include any trailing white space) :

Main-Class: com.domain.progs.Prog2

Lastly, add everything to a jar file. I don't remember the exact syntax to use but I know it starts with this:

jar cvmf ...

Just look up the jar command syntax and make sure to include the manifest file and the com directory that has the class files, not the java ones

That's it. You can now run your jar file. To do that type this command:

java -jar JarFileName.jar

PS If you want something you can double click to run, it must be a GUI application; otherwise it won't start a console to show your program in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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