-1

Here is the makefile code.

Spell_Checker.class: Dictionary.class Spell_Checker.java
    javac Spell_Checker.java

Dictionary.class: Dictionary.java
    javac Dictionary.java

Here is the error log for the makefile when I type make inside of terminal.

javac Dictionary.java
Picked up _JAVA_OPTIONS: -Xmx32m
javac Spell_Checker.java
Picked up _JAVA_OPTIONS: -Xmx32m
Spell_Checker.java:26: error: cannot find symbol
        Dictionary spell_check_this = new Dictionary(DICT_FILE);
        ^
symbol:   class Dictionary
location: class Spell_Checker
Spell_Checker.java:26: error: cannot find symbol
        Dictionary spell_check_this = new Dictionary(DICT_FILE);
                                          ^
symbol:   class Dictionary
location: class Spell_Checker 2 errors
makefile:2: recipe for target 'Spell_Checker.class' failed
make: *** [Spell_Checker.class] Error 1

I'm new to makefile and I have a java file called Spell_Checker.java that calls Dictionary.java to check a file if it's spelled correctly. I'm having troubles figuring out how to write the makefile correctly. Thanks!

JWill23
  • 87
  • 2
  • 12
  • Possible duplicate of [Import a custom class in Java](https://stackoverflow.com/questions/7869006/import-a-custom-class-in-java) – Cardinal System Feb 11 '18 at 21:36
  • 2
    Posting images of code and errors is frowned upon here, because it makes it more difficult for us to help you with your issue. It is always better to copy and paste the relevant code and/or errors into your question directly. Please read [Why not to upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-to-upload-images-of-code-on-so-when-asking-a-question), then [edit] your question accordingly. – Joe C Feb 11 '18 at 21:36
  • 2
    I suggest using an IDE first and then using a build tool designed for Java such as maven or gradle and you won't need to write rules like this. – Peter Lawrey Feb 11 '18 at 21:39
  • I'm not allowed to use an IDE to do this unfortunately. I edited the question to add the copied code and error. – JWill23 Feb 11 '18 at 21:49
  • 3
    I think it should be `javac -cp . Spell_Checker.java` in order for it to be able to find the other class. Seems like this question has most probably nothing to do with `make`: if you reproduce without `make` (ie: using `javac` from the shell) it might be easier to find help, as I don't think many people use make with java – giorgiga Feb 11 '18 at 21:53
  • Ok no IDE, but how about Maven or Gradle? – Franklin Yu Feb 11 '18 at 23:09

2 Answers2

0

You need to specify a CLASSPATH to search along with your compiler. The usual way is to assign variables for things like that in a Makefile. I would also always provide "all" and "clean" targets. That might look something like,

JAVAC = javac
CLASSPATH = -cp .

all: Dictionary.class Spell_Checker.class

clean:
        rm -f *.class

Dictionary.class: Dictionary.java
        $(JAVAC) $(CLASSPATH) Dictionary.java

Spell_Checker.class: Dictionary.class Spell_Checker.java
        $(JAVAC) $(CLASSPATH) Spell_Checker.java
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

It seems that your classes are in a package but your source files aren't in a corresponding directory. Say for example you have:

 package mypackage;

Then your source files should be in the directory mypackage, and your makefile should read:

mypackage/Dictionary.class: mypackage/Dictionary.java
    javac $?

And so forth.

user207421
  • 305,947
  • 44
  • 307
  • 483