I know I'm doing something stupid here. I'm trying to compile java from the command line with java/javac. I've looked through many answers to this and just can't get it. I can compile and run our good friend HelloWorld.java with no problems. I'm trying to compile code from Java 8 In Action. The .java file is:
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 +
'}';
}
}
}
I compiled it with "javac -cp . FilterApples.java" and it compiles with no errors or warnings. I get two class files: FilteringApples$Apple.class FilteringApples.class. I'v tried various ways to run it including "java -cp . FilterApples" and "java -cp . lambdasinaction.chap1.FilteringApples". It always comes back with
Error: Could not find or load main class lambdasinaction.chap1.FilteringApples
Output from javap is:
Compiled from "FilteringApples.java"
public class lambdasinaction.chap1.FilteringApples {
public lambdasinaction.chap1.FilteringApples();
public static void main(java.lang.String[]);
...
It was suggested that this is a duplicate of another question. I looked at that question in researching this one and the suggestions didn't work. Also, As I said, I can compile HelloWorld.java with no problems. I think there is something different here.
What am I doing wrong?
Thanks
NOTE: "java HelloWorld" worked fine but java "FilterApples" failed in same directory because HelloWorld has no package statement but FilterApples does:
package lambdasinaction.chap1;