intelliJ IDEA 运行 class 带命令行参数
intelliJ IDEA run class with command line argument
我正在阅读 Sedgewick 的《算法》一书,但我似乎无法编写我的 IDE 运行 他们的程序。程序启动但不会接受传递的参数。具体来说,我希望它打开我在程序参数部分设置的 tiny.txt 文件,但它只是被忽略了...
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class Selection
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length; // array length
for (int i = 0; i < N; i++)
{ // Exchange a[i] with smallest entry in a[i+1...N).
int min = i; // index of minimal entr.
for (int j = i+1; j < N; j++)
if (less(a[j], a[min])) min = j;
exch(a, i, min);
} }
// See page 245 for less(), exch(), isSorted(), and main().
private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
private static void exch(Comparable[] a, int i, int j)
{ Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
private static void show(Comparable[] a)
{ // Print the array, on a single line.
for (int i = 0; i < a.length; i++)
StdOut.print(a[i] + " ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{ // Test whether the array entries are in order.
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
public static void main(String[ ] args)
{ // Read strings from standard input, sort them, and print.
String[] a = In.readStrings();
sort(a);
assert isSorted(a);
show(a);
}
}
您似乎需要标准输入重定向。不幸的是,IntelliJ 不支持。
当您提供命令行参数时,所做的只是将参数中的每个单词作为字符串传递到 main
方法的 args
参数中。然后,您可以在 main
中编写一些代码以在该文件上打开 BufferedReader。
替代方法是打开终端 window(或命令提示符 window)并键入命令行 java package.name.ClassName < filename.ext
。命令处理器或 shell 将 <
字符解释为将标准输入重定向到提供的文件的请求。
好的,如果您想在 mac OS Sierra 上使用 intelliJ IDEA 中的算法代码,则需要完成以下操作:
1,确保从他们的网站
获取 运行 algs4.app
2,使用 intelliJ 终端导航到 algorithms2/out/production/algorithms2 .class 文件所在的位置
3、在终端输入:$java-algs4 Selection
我正在阅读 Sedgewick 的《算法》一书,但我似乎无法编写我的 IDE 运行 他们的程序。程序启动但不会接受传递的参数。具体来说,我希望它打开我在程序参数部分设置的 tiny.txt 文件,但它只是被忽略了...
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class Selection
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length; // array length
for (int i = 0; i < N; i++)
{ // Exchange a[i] with smallest entry in a[i+1...N).
int min = i; // index of minimal entr.
for (int j = i+1; j < N; j++)
if (less(a[j], a[min])) min = j;
exch(a, i, min);
} }
// See page 245 for less(), exch(), isSorted(), and main().
private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
private static void exch(Comparable[] a, int i, int j)
{ Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
private static void show(Comparable[] a)
{ // Print the array, on a single line.
for (int i = 0; i < a.length; i++)
StdOut.print(a[i] + " ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{ // Test whether the array entries are in order.
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
public static void main(String[ ] args)
{ // Read strings from standard input, sort them, and print.
String[] a = In.readStrings();
sort(a);
assert isSorted(a);
show(a);
}
}
您似乎需要标准输入重定向。不幸的是,IntelliJ 不支持。
当您提供命令行参数时,所做的只是将参数中的每个单词作为字符串传递到 main
方法的 args
参数中。然后,您可以在 main
中编写一些代码以在该文件上打开 BufferedReader。
替代方法是打开终端 window(或命令提示符 window)并键入命令行 java package.name.ClassName < filename.ext
。命令处理器或 shell 将 <
字符解释为将标准输入重定向到提供的文件的请求。
好的,如果您想在 mac OS Sierra 上使用 intelliJ IDEA 中的算法代码,则需要完成以下操作:
1,确保从他们的网站
获取 运行 algs4.app2,使用 intelliJ 终端导航到 algorithms2/out/production/algorithms2 .class 文件所在的位置
3、在终端输入:$java-algs4 Selection