0

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;
steven smith
  • 1,519
  • 15
  • 31
  • Pls check if you need to update the inner class https://stackoverflow.com/questions/7486012/static-classes-in-java – Sunil Kumar Oct 15 '18 at 18:25
  • 1
    Which directory are you in when you run `java -cp . lambdasinaction.chap1.FilteringApples`? You need to be in the directory _above_ the one called `lambdasinaction`. – Dawood ibn Kareem Oct 15 '18 at 18:31
  • 1
    You run the java command in the wrong directory, using the wrong class name. Nicely explained in that duplicate question. And: javap is less sensitive. javap doesn't care about the class path. It just shows you what is in your files. The JVM isn't that robust. – GhostCat Oct 15 '18 at 18:36

1 Answers1

3

On Windows:

Assuming the current directory is C:\Temp\sample

Step 1

Create directories lambdasinaction\chap1 and put file FilteringApples.java there

Step 2

While being in C:\Temp\sample compile the code: javac lambdasinaction\chap1\FilteringApples.java. After this step, the expected output is:

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

Step 3

While being in C:\Temp\sample run the code: java lambdasinaction.chap1.FilteringApples

The expected output is:

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}]
[]
jpw
  • 44,361
  • 6
  • 66
  • 86
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • I'm gettng spoiled by using an IDE I'm afraid. "for x in `find . -name \*.java`; do javac $x; done` compiled from the right directory compiled everything just fine :) Thanks ALL! – steven smith Oct 15 '18 at 18:50