gpt4 book ai didi

javac:无法找到或加载主类

转载 作者:行者123 更新时间:2023-12-01 19:47:05 24 4
gpt4 key购买 nike

我知道我在这里做了一些愚蠢的事情。我正在尝试使用 java/javac 从命令行编译 java。我已经浏览了很多答案,但无法得到它。我可以毫无问题地编译并运行我们的好 friend HelloWorld.java。我正在尝试从Java 8 In Action编译代码。 .java 文件是:

package lambdasinaction.chap1;
import java.util.*;
import java.util.function.Predicate;
public class FilteringApples{
// public static void main(String ... args){
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
new Apple(155, "green"),
new Apple(120, "red"));
List<Apple> greenApples = filterApples(
inventory, FilteringApples::isGreenApple);
System.out.println(greenApples);
List<Apple> heavyApples = filterApples(
inventory, FilteringApples::isHeavyApple);
System.out.println(heavyApples);
List<Apple> greenApples2 = filterApples(inventory,
(Apple a) -> "green".equals(a.getColor()));
System.out.println(greenApples2);
List<Apple> heavyApples2 = filterApples(inventory,
(Apple a) -> a.getWeight() > 150);
System.out.println(heavyApples2);
List<Apple> weirdApples = filterApples(inventory,
(Apple a) -> a.getWeight() < 80 || "brown".equals(a.getColor()));
System.out.println(weirdApples);
}
public static List<Apple> filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if ("green".equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}
public static List<Apple> filterHeavyApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for (Apple apple: inventory){
if (apple.getWeight() > 150) {
result.add(apple);
}
}
return result;
}
public static boolean isGreenApple(Apple apple) {
return "green".equals(apple.getColor());
}
public static boolean isHeavyApple(Apple apple) {
return apple.getWeight() > 150;
}
public static List<Apple> filterApples(
List<Apple> inventory, Predicate<Apple> p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
public static class Apple {
private int weight = 0;
private String color = "";
public Apple(int weight, String color){
this.weight = weight;
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
'}';
}
}
}

我用“javac -cp . FilterApples.java”编译它,它编译时没有错误或警告。我得到两个类文件:FilteringApples$Apple.class FilteringApples.class。我尝试了各种方法来运行它,包括“java -cp . FilterApples”和“java -cp . lambdasinaction.chap1.FilteringApples”。它总是回来

Error: Could not find or load main class lambdasinaction.chap1.FilteringApples

javap 的输出是:

Compiled from "FilteringApples.java"
public class lambdasinaction.chap1.FilteringApples {
public lambdasinaction.chap1.FilteringApples();
public static void main(java.lang.String[]);
...

有人建议这是一个 duplicate of another question 。我在研究这个问题时查看了这个问题,但这些建议不起作用。另外,正如我所说,我可以毫无问题地编译 HelloWorld.java。我认为这里有些不同。

我做错了什么?
谢谢

注意:“java HelloWorld”工作正常,但 java“FilterApples”在同一目录中失败,因为 HelloWorld 没有包声明,但 FilterApples 有:

package lambdasinaction.chap1;

最佳答案

在 Windows 上:

假设当前目录为C:\Temp\sample

第 1 步

创建目录lambdasinaction\chap1并将文件FilteringApples.java放在那里

第 2 步

C:\Temp\sample 中编译代码:javac lambdasinaction\chap1\FilteringApples.java。执行此步骤后,预期输出为:

C:\temp\sample>tree /F
C:.
└───lambdasinaction
└───chap1
FilteringApples$Apple.class
FilteringApples.class
FilteringApples.java

第3步

C:\Temp\sample 中运行代码:java lambdasinaction.chap1.FilteringApples

预期输出是:

C:\temp\sample>java lambdasinaction.chap1.FilteringApples
[Apple{color='green', weight=80}, Apple{color='green', weight=155}]
[Apple{color='green', weight=155}]
[Apple{color='green', weight=80}, Apple{color='green', weight=155}]
[Apple{color='green', weight=155}]
[]

关于javac:无法找到或加载主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52822654/

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