I have a Java program, and write a makefile to compile it on Linux.
My project organized like this (Run.java is the main entry)
Program -
Src -
(package)adb.Bing_WebResults
Run.java
(package)adb.jsonModel
*.java
(package)adb.models
*.java
bin -
lib -
gson.jar
commons.jar
resource -
*.txt
This is my makefile:
# My project require 3 parameters from user input.
default: Run.class
Run.class: src/adb/Bing_WebResults/Run.java
javac -sourcepath src/ -classpath lib/*.jar -d bin/ src/adb/Bing_WebResults/*.java src/adb/jsonModels/*.java src/adb/models/*.java
run:
java -classpath bin/:lib/*.jar Run "$(ARG1)" "$(ARG2)" "$(ARG3)"
When I use “make run” command in Linux terminate, exception shows that “Could not find the main class: Run”
Are there something wrong with my makefile? Wrong path or something?
There are many things that could potentially be wrong, but the most apparent issues are the incorrect dependencies of the targets in your makefile.
First of all, the target
runshould have a dependency onRun.class. If you domake runthenmakelooks at the target calledrun. In yourmakefile, this target does not have any dependencies defined, and it will execute the linejava ...without checking whether the actual compiled classRun.classexists. As a consequence, if you domake runfrom a clean situation, your source code will not be compiled and thejavacommand will fail because the compiled class is missing.Your dependency of
defaultonRun.classis incorrect as well, becauseRun.classwill exist in thebindirectory, not in the working directory. The line below mentions the targetRun.classas well.There are several ways to improve your makefile. See below an example of corrected code with some variables added to avoid repeated expressions. This approach is a matter of style and preference though.
This works for me in a simplified, comparable setup — it might work for you as well. Looking at the snippet you provided, there are most likely other dependencies or changes that need to be added to complete your
makefilecorrectly. Potentially, you might have to add package information to your run command and dependency expressions, but that depends on your source code. Your post does not contain enough information to provide a complete solution.P.S.: Do not forget to replace spaces by tabs if you copy this code to your own
makefile.