在同一控制台中从 java 调用和 运行 一个 jar 文件
Calling and running a jar file from java in the same console
我正在尝试编写一个能够从给定目录打开任何 .jar 文件的程序。使用 Process ps = Runtime.getRuntime().exec("java -jar " + contents[selectInt - 1]);
,其中 contents[selectInt - 1]
的典型值类似于 C:\Users\Kieran\Desktop\CharCount.jar
(我已经测试过,它确实给出了 jar 文件的完整文件路径),没有命令提示符 window弹出,当前window中什么也没有出现。在控制台中输入 java -jar C:\Users\Kieran\Desktop\CharCount.jar
就可以了。完整代码如下:
package listfiles;
import java.util.Scanner;
import java.io.*;
public class ListFiles {
private static Scanner sc = new Scanner(System.in);
private static File folder;
public static void main(String[] args) {
//Set folder to search
String filePath;
final int LENGTH = args.length;
if (LENGTH > 0) {
filePath = args[0];
} else {
System.out.print("File to search (or \"QUIT\" to exit): ");
filePath = sc.next();
if (filePath.equals("QUIT")) {
exit();
}
}
folder = new File(filePath);
//Create an array of folder contents with the extension .jar and print it
File[] contents = folder.listFiles(new JarFilter());
System.out.print("Search in \"" + filePath + "\" found " + contents.length + " result");
if (contents.length != 1) {
System.out.print("s");
}
System.out.print(":\n\n");
int i = 0;
for (File file : contents) {
i++;
System.out.println("[" + i + "] " + file);
}
//Allow the user to run a jar file
int selectInt = 0;
while (true) {
System.out.print("Please select a file number to open or \"QUIT\" to exit: ");
String select = sc.next();
if (select.equals("QUIT")) {
exit();
}
try {
selectInt = Integer.parseInt(select);
} catch(NumberFormatException e) {
continue;
}
if ((selectInt > 0) && (selectInt <= contents.length)) {
break;
}
}
try {
System.out.println("java -jar " + contents[selectInt - 1]);
Process ps = Runtime.getRuntime().exec("java -jar " + contents[selectInt - 1]);
} catch (IOException e) {
System.out.println("Opening .jar file failed.");
exit();
}
}
private static void exit() {
System.out.print("\n\nExiting...\n");
System.exit(0);
}
}
JarFilter 只接受 .jar 文件(并且有效)。
如果能提供任何帮助,我将不胜感激。
编辑:CharCount.jar 的代码如下:
package charcount;
public class CharCount
{
public static void main(String[] args)
{
int i = 0;
for (String str : args)
{
System.out.println("\"" + str + "\" has " + str.length() + " characters.");
i += str.length();
}
System.out.println("Total characters: " + i);
System.exit(0);
}
}
如果您尝试从 cmd 运行 它,您是否尝试过:
Runtime.getRuntime().exec("cmd /c start java -jar FILEPATH");
或者您的情况:
Runtime.getRuntime().exec("cmd /c start java -jar" + contents[selectInt - 1]);
----更新----
要保持提示打开,请使用:
Runtime.getRuntime().exec("cmd /c start cmd /k java -jar " + contents[selectInt - 1]);
我正在尝试编写一个能够从给定目录打开任何 .jar 文件的程序。使用 Process ps = Runtime.getRuntime().exec("java -jar " + contents[selectInt - 1]);
,其中 contents[selectInt - 1]
的典型值类似于 C:\Users\Kieran\Desktop\CharCount.jar
(我已经测试过,它确实给出了 jar 文件的完整文件路径),没有命令提示符 window弹出,当前window中什么也没有出现。在控制台中输入 java -jar C:\Users\Kieran\Desktop\CharCount.jar
就可以了。完整代码如下:
package listfiles;
import java.util.Scanner;
import java.io.*;
public class ListFiles {
private static Scanner sc = new Scanner(System.in);
private static File folder;
public static void main(String[] args) {
//Set folder to search
String filePath;
final int LENGTH = args.length;
if (LENGTH > 0) {
filePath = args[0];
} else {
System.out.print("File to search (or \"QUIT\" to exit): ");
filePath = sc.next();
if (filePath.equals("QUIT")) {
exit();
}
}
folder = new File(filePath);
//Create an array of folder contents with the extension .jar and print it
File[] contents = folder.listFiles(new JarFilter());
System.out.print("Search in \"" + filePath + "\" found " + contents.length + " result");
if (contents.length != 1) {
System.out.print("s");
}
System.out.print(":\n\n");
int i = 0;
for (File file : contents) {
i++;
System.out.println("[" + i + "] " + file);
}
//Allow the user to run a jar file
int selectInt = 0;
while (true) {
System.out.print("Please select a file number to open or \"QUIT\" to exit: ");
String select = sc.next();
if (select.equals("QUIT")) {
exit();
}
try {
selectInt = Integer.parseInt(select);
} catch(NumberFormatException e) {
continue;
}
if ((selectInt > 0) && (selectInt <= contents.length)) {
break;
}
}
try {
System.out.println("java -jar " + contents[selectInt - 1]);
Process ps = Runtime.getRuntime().exec("java -jar " + contents[selectInt - 1]);
} catch (IOException e) {
System.out.println("Opening .jar file failed.");
exit();
}
}
private static void exit() {
System.out.print("\n\nExiting...\n");
System.exit(0);
}
}
JarFilter 只接受 .jar 文件(并且有效)。
如果能提供任何帮助,我将不胜感激。
编辑:CharCount.jar 的代码如下:
package charcount;
public class CharCount
{
public static void main(String[] args)
{
int i = 0;
for (String str : args)
{
System.out.println("\"" + str + "\" has " + str.length() + " characters.");
i += str.length();
}
System.out.println("Total characters: " + i);
System.exit(0);
}
}
如果您尝试从 cmd 运行 它,您是否尝试过:
Runtime.getRuntime().exec("cmd /c start java -jar FILEPATH");
或者您的情况:
Runtime.getRuntime().exec("cmd /c start java -jar" + contents[selectInt - 1]);
----更新----
要保持提示打开,请使用:
Runtime.getRuntime().exec("cmd /c start cmd /k java -jar " + contents[selectInt - 1]);