我希望这个程序能够不断启动自己的新实例。这个程序没有任何实际功能,我只是很好奇。我能够让它找到自己的文件路径,但是当我使用Runtime类并使用该文件路径执行它时,没有任何反应。我也尝试使用ProcessBuilder,但仍然没有发生任何事情。我确保把它构建成一个jar并执行它,因为我知道如果我在编辑器中运行它就不会工作。
public class Main
{
public static void main(String[] args) throws Exception
{
System.out.println("Started");
CodeSource codeSource = Main.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
System.out.println("File: " + jarFile.toURI().toString());
Runtime.getRuntime().exec(("java -jar "+jarFile.toURI().toString()));
}
}
答案 0 :(得分:0)
此课程将自行重启100次:
public class Test {
static int counter = 0;
public static void main(String[] args) {
while (counter < 100) {
System.out.println("run counter =" + counter++);
ReRun.start();
}
}
public static class ReRun {
public static void start() {
Test.main(null);
}
}
}
答案 1 :(得分:0)
我不确定这是否对您有帮助,但这是另一种简单的技术,除非您在主菜单上选择退出选项或输入错误的输入,否则我的java类会反复执行自身。
在这里,如果您看到代码,我所做的就是使用Switch case语句创建可执行的选择选项的基本Area finder程序。
从System.out.println()
开始,我在其中打印了正方形,圆形,三角形等区域,我创建了一个无限for循环,如下面的代码所示
并且在主要switch case语句大括号之后,无限for循环大括号结束。因为如果您注意到有2个switch case语句。一个用于主要选项,另一个用于“您是否要继续?”部分,这是我给另外2个选项的地方。如果用户点击1,则继续执行for循环,如果用户点击0,则将执行System.exit(0);
,这将终止无限循环,从而导致整个代码终止。
public static void main(String[] args) {
double sqArea, reArea = 0, cirArea = 0, trArea = 0;
double base, height, length, breadth, side, radius;
Scanner in = new Scanner(System.in);
for (int i = 1; i >= 1; i++) {
System.out.println("--------Area Finder---------");
System.out.println("1.Area of a Square");
System.out.println("2.Area of a Rectangle");
System.out.println("3.Area of a Circle");
System.out.println("4.Area of a Triangle");
System.out.println("5.Exit");
System.out.println("----------------------------");
System.out.println("");
System.out.println("Choose your option:");
int option = in.nextInt();
switch (option) {
case 1:
System.out.println("-----Area of a Square-----");
System.out.println("");
System.out.println("Enter the value of sides");
side = in.nextDouble();
sqArea = side * side;
System.out.println("Therefore the area of a square is " + sqArea);
System.out.println("-------------------------------------------");
break;
case 2:
System.out.println("-----Area of a Rectangle-----");
System.out.println("");
System.out.println("Enter the value of length");
length = in.nextDouble();
System.out.println("Enter the value of breadth");
breadth = in.nextDouble();
reArea = length * breadth;
System.out.println("Therefore the area of a rectangle is " + reArea);
System.out.println("-------------------------------------------");
break;
case 3:
System.out.println("-----Area of a Circle-----");
System.out.println("");
System.out.println("Enter the value of radius");
radius = in.nextDouble();
cirArea = 3.14 * radius * radius;
System.out.println("Therefore the area of a circle is " + cirArea);
System.out.println("-------------------------------------------");
break;
case 4:
System.out.println("-----Area of a Triangle-----");
System.out.println("");
System.out.println("Enter the value of base");
base = in.nextDouble();
System.out.println("Enter the value of height");
height = in.nextDouble();
trArea = 0.5 * base * height;
System.out.println("Therefore the area of a triangle is " + trArea);
System.out.println("-------------------------------------------");
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice -> Choose Only from 1 to 5");
System.out.println("Do you wish to continue? (1 for yes | 0 for no)");
int opt = in.nextInt();
switch (opt) {
case 1:
continue;
case 0:
System.exit(0);
break;
}
}
}
}