[英]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.