如何实现双击JAR文件时自动打开Windows命令提示符运行程序
问题描述
我是一名Java新手,有一周Java开发经验和三周C#开发经验,正在做第一个基础项目——一个靠IntelliJ运行、终端交互的简易计算器。已经把项目导出成JAR文件,现在需要通过Windows命令提示符打开运行。我希望双击这个JAR文件时,能自动打开命令提示符并运行程序,而且只能依赖单个JAR文件,不能用额外的批处理文件之类的。之前搜过相关问题,但找到的方案要么没用,要么需要多个文件,试过的搜索关键词包括“How to open non java files with java”“How to open external files with java”“How to run Windows Command prompt with java”,也看了YouTube和其他相关问题,都没找到可行的办法。
主Java文件代码
import java.util.Scanner; public class Main { public static void main(String[] args) { // Make it automatically open Command prompt float[] inputVariables = new float[4]; calculator(inputVariables); } public static void calculator(float[] loopVariables) { System.out.println("WARNING: Inputting a non true or false value, or a non number when asked to will crash the program! (For now)"); Scanner inputScanner = new Scanner(System.in); while (true) { float z = 0; System.out.println("Select an operation: (Addition: +, Subtraction: -, Multiplication: *, Division: /, Modulo: %, Power: ^, Absolute Value: |, Round: ~"); String operation = inputScanner.next(); switch (operation) { case "+", "-", "*", "/", "%", "^", "|", "~" -> { } default -> { System.out.println("This is not a valid operation."); continue; } } float x; if (loopVariables[1] == 0) { System.out.println("Input x:"); x = inputScanner.nextFloat(); } else { x = loopVariables[0]; System.out.println("x = " + x); } float y = 0; switch (operation) { case "+", "-", "*", "/", "%", "^" -> { System.out.println("Input y:"); y = inputScanner.nextFloat(); } case "~" -> { System.out.println("Round up, down or auto: (+ or -)"); operation = inputScanner.next(); switch (operation) { case "+" -> { System.out.println("+~" + x + " = " + (Math.ceil(x))); z = (float)Math.ceil(x); operation = "~"; } case "-" -> { System.out.println("-~" + x + " = " + (Math.floor(x))); z = (float)(Math.floor(x)); operation = "~"; } default -> { System.out.println("This is not a valid operation."); continue; } } } case "|" -> { } } switch (operation) { case "+" -> { System.out.println(x + " + " + y + " = " + (x + y)); z = x + y; } case "-" -> { System.out.println(x + " - " + y + " = " + (x - y)); z = x - y; } case "*" -> { System.out.println(x + " * " + y + " = " + (x * y)); z = x * y; } case "/" -> { System.out.println(x + " / " + y + " = " + (x / y)); z = x / y; } case "%" -> { System.out.println(x + " % " + y + " = " + (x % y)); z = x % y; } case "^" -> { System.out.println(x + "^" + y + " = " + (float)(Math.pow(x, y))); z = (float)Math.pow(x, y); } case "|" -> { System.out.println("|" + x + "| = " + (Math.abs(x))); z = Math.abs(x); } case "~" -> { } default -> { System.out.println("This is not a valid operation."); continue; } } System.out.println("Would you like to continue with the result as x? (true or false)"); boolean finish = inputScanner.nextBoolean(); float hasLooped; if (finish) { hasLooped = 1; loopVariables[0] = z; loopVariables[1] = hasLooped; continue; } else { System.out.println("Would you like to continue without the result as x? (true or false)"); finish = inputScanner.nextBoolean(); } hasLooped = 0; loopVariables[1] = hasLooped; if (!finish) { System.out.println("Ending Calculator."); break; } } } }
内容的提问来源于stack exchange,提问作者Zadezapper
相关产品推荐
相关产品推荐

