1

I have a bash file which should repeatedly run this java program for data collection while I sleep.

Here is bash file:

#!/bin/bash

javac Main.java

START=`date +%s`
while [ $(( $(date +%s) - 28800 )) -lt $START ]; do
    java Main
done

When I execute the bash file: ./cache_script.sh from the same directory as my Main.java file, these errors occurs:

Main.java:16: error: cannot find symbol
        Entry[] entries = new Entry[DATA_SET_SIZE];
        ^
  symbol:   class Entry
  location: class Main
Main.java:16: error: cannot find symbol
        Entry[] entries = new Entry[DATA_SET_SIZE];
                              ^
  symbol:   class Entry
  location: class Main
Main.java:20: error: cannot find symbol
            entries[i] = new Entry(999999999, 999999999, 99999999.9);
                             ^
  symbol:   class Entry
  location: class Main
Main.java:25: error: cannot find symbol
            LruCache<Integer, Entry> lruCache = new LruCache<>(CAPACITY);
            ^
  symbol:   class LruCache
  location: class Main
Main.java:25: error: cannot find symbol
            LruCache<Integer, Entry> lruCache = new LruCache<>(CAPACITY);
                              ^
  symbol:   class Entry
  location: class Main
Main.java:25: error: cannot find symbol
            LruCache<Integer, Entry> lruCache = new LruCache<>(CAPACITY);
                                                    ^
  symbol:   class LruCache
  location: class Main
6 errors

It looks like the bash interpreter isn't finding the Entry.java and LruCache.java files that my Main.java uses.

How can I fix this?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54

2 Answers2

2

You need to specify your classpath additionaly. To do so you need to add "-cp %FOLDER_WITH_CLASS_FILES%" attribute to your "java Main" call

In general "java" call knows nothing about where it should look for LruCache, Entry and all other classes. And classpath attribute gives such clue.

You can read more here about classpath attribute here http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

1

You should add all used classes / *jars to your classpath while compiling with javac and then while running with java commands.

See here: how to compile java package structures using javac how to do that with various options depending on your project structure

Ivan Pronin
  • 1,768
  • 16
  • 14
  • I am still confused, I have made these changes but it still doesn't work: `javac Entry.java -cp /Users/tomfinet/IdeaProjects/CacheExperiment/src/com/ee/ javac LruCache.java -cp /Users/tomfinet/IdeaProjects/CacheExperiment/src/com/ee/ javac Main.java -cp /Users/tomfinet/IdeaProjects/CacheExperiment/src/com/ee/` And instead of `java Main` I have `java Main -cp /Users/tomfinet/IdeaProjects/CacheExperiment/src/com/ee/` – Tom Finet Aug 13 '17 at 21:31