gpt4 book ai didi

java错误: Could not find or load main class BinarySearch

转载 作者:行者123 更新时间:2023-11-30 05:29:43 25 4
gpt4 key购买 nike

我正在阅读 Sedgewick 的《算法》(第四版)。代码是这样的:

package edu.princeton.cs.algs4;

import java.util.Arrays;

/**
* The {@code BinarySearch} class provides a static method for binary
* searching for an integer in a sorted array of integers.
* <p>
* The <em>indexOf</em> operations takes logarithmic time in the worst case.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/11model">Section 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BinarySearch {

private BinarySearch() { }
public static int indexOf(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}

@Deprecated
public static int rank(int key, int[] a) {
return indexOf(a, key);
}

public static void main(String[] args) {

// read the integers from a file
In in = new In(args[0]);
int[] whitelist = in.readAllInts();

// sort the array
Arrays.sort(whitelist);

// read integer key from standard input; print if not in whitelist
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (BinarySearch.indexOf(whitelist, key) == -1)
StdOut.println(key);
}
}
}

我已经成功编译了代码:

javac -cp /Users/user/documents/algorithms/algs4-master/target/algs4-1.0.0.0.jar BinarySearch.java

但是当我尝试运行代码时,发生错误:

user$ java -cp /Users/user/documents/algorithms/algs4-master/target/algs4-1.0.0.0.jar BinarySearch Error: Could not find or load main class BinarySearch Caused by: java.lang.ClassNotFoundException: BinarySearch

请帮忙告诉我这里发生了什么。非常感谢!

最佳答案

提供 main 方法所在类的整个包,并且不带 .java,因为您正在运行该类,而不是 java 文件:

edu.princeton.cs.algs4

javac -cp /Users/user/documents/algorithms/algs4-master/target/algs4-1.0.0.0.jar edu.princeton.cs.algs4.BinarySearch

关于java错误: Could not find or load main class BinarySearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57808935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com