简体   繁体   English

我如何制作可以同时运行这两个课程的第三堂课?

[英]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. 您的类没有main功能,因此无法执行。 But if they did have main functions: 但是,如果它们确实具有main功能:

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; (即Prog2.java和Prog3.java)。它们也位于同一软件包中,因此您将添加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: 因此,将其放在Prog2的main功能中:

Prog3.main();

Third, you need to put the whole package in a jar file. 第三,您需要将整个程序包放在jar文件中。 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. 编译文件,使输出(或bin等)看起来像^,但带有类文件而不是Java文件。

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) : 将其输入到名为MANIFEST.MF的文件中(确保不包含任何结尾的空格)

Main-Class: com.domain.progs.Prog2

Lastly, add everything to a jar file. 最后,将所有内容添加到jar文件中。 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 只需查找jar命令语法,并确保包括清单文件和包含类文件的com目录,而不是Java文件

That's it. 而已。 You can now run your jar file. 您现在可以运行jar文件。 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; PS:如果您想要某些东西,可以双击运行,它必须是一个GUI应用程序。 otherwise it won't start a console to show your program in 否则,它不会启动控制台以显示您的程序

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

相关问题 提示找不到或加载主类时如何运行该程序? - How can I make this program run when it says could not find or load main class? 能否将在两个独立类中创建的对象组合起来,在第三个类中创建第三个对象? - Can you combine objects created in two separate classes to create a third object in a third class? 如何将接口设为私有,但在两个类之间使用接口? - How can I make an Interface Private but used between two classes? 如何将一班变成两班 - How to make one class into two classes 当前两个已经处于基-子类关系中时,如何继承第三个类中的两个类? - How to inherit two classes in a third class when the first two are in base-child class relationship already? 一个类如何在Java中扩展两个类? - How can a class extend two classes in Java? 如何让java中的testng对象运行列表并生成reportNG网页? - How can I make testng object run list of classes in java and make reportNG webpage? 如何同时运行两个不同的主类? - How can I run two different main class at the same time? 如何让Ant Javadoc类排除两个文件? - How can I make an Ant Javadoc class exclude two files? Java:如何在第三类中创建两个不同类的实例,并通过构造函数相互传递引用 - Java: how to create instances of two different classes in a third class and pass references to each other through the constructors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM