There are several questions with this title and I looked over them but did not find a solution. I am trying to create a MakeFile for my java project so that the prof can run it easily in unix environment. Here is the content of my makefile file:
JFLAGS = -g
JC = javac
JVM= java
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
DivisorStorage.java\
countingNumberOfDivisorsRunnable.java\
HighestDivisor.java\
MAIN = HighestDivisor
default: classes
classes: $(CLASSES:.java=.class)
run: classes
$(JVM) $(MAIN)
clean:
$(RM) *.class
and then i am tying "make -f makefile" but this error comes up "make: Nothing to be done for `default'"
am i even using the right command to run the program?"make -f makefile"
Any help is appreciated! Thank you all for your time!
Related
I'm building a java program in Windows envoirment, but i need to be able to compile it in Linux envoirment. I'm currently having problems with the makefile:
the project folders are organized in this way:
project_folder
library.jar
src
folder1
a.java
b.java
folder2
c.java
d.java
folder3
e.java
The e.java file into folder3 is in almost all the others .java files because it's the principal struct, and for every file i have the line
import folder3.e;
Wich Eclipse automatically generates when i use a structure from another class file. What the terminal tells me:
error: Package folder3 does not exist
this is the makefile
JFLAGS = -g -cp library.jar -sourcepath .
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
src/folder1/a.java \
src/folder1/b.java \
src/folder2/c.java \
src/folder2/d.java \
src/folder3/e.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
find . -name "*.class" -type f -delete
It's my very first makefile for java and i think i'm wrongly including the references between classes, how i can solve this?
Thank you
UPDATE: #Karthikeyan Vaithilingam thank you for your detailed answer, now it's all clear and working!
Add -d . to JFLAGS so the class files will be created with proper folder structure.
So your JFLAGS should be as follows.
JFLAGS = -g -cp .:library.jar -sourcepath . -d .
-d is the flag is used for destination folder in the above example its the current folder. You can use any destination but don't forget to add it to the classpath using -cp. By using -d you can keep the source folders clean, also cleaning will be easy, just remove the destination folder.
Update
The problem is the order of the Java files in CLASSES section. Since folder3.e class is referred in other classes it has to be at the beginning. It should be like the below one.
CLASSES = \
src/folder3/e.java \
src/folder1/a.java \
src/folder1/b.java \
src/folder2/c.java \
src/folder2/d.java
Also you can keep the libraries in a separate folder then use -cp libs\* to add all the jars in the folder. It will be easy to maintain
So the folder structure will be
project_folder
libs
library.jar
src
folder1
a.java
b.java
folder2
c.java
d.java
folder3
e.java
And the final Makefile will be
JFLAGS = -g -cp ".:libs/*" -sourcepath . -d .
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
src/folder3/e.java \
src/folder1/a.java \
src/folder1/b.java \
src/folder2/c.java \
src/folder2/d.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
rm -r folder*
Unrelated
Why don't you use build tools Gradle or Maven or Ant?
I am trying to make a makefile for compiling a single java class on linux. I made a makefile for it, as follows:
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
des.java \
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
This gives me the error Makefile:9: *** multiple target patterns. Stop.
Where am I going wrong?
Thanks.
You have a stray line-continuation marker on the des.java \ line.
This is causing make to assign the value des.java default: classes to the CLASSES variable.
When make then expands that on the classes: $(CLASSES:.java=.class) line it sees multiple colons and gets confused.
I have a project that I created with eclipse.
Now I want to copy this project to my linux computer to compile and run it there.
For this I want to create a makefile for compiling and running automatically.
I already have created a makefile and it can compile my project.
But now it should start my program after compiling and I dont know how to make this.
I want to type "make" and it should compile the source and after that it should start my main automatically. For now I have a shellscript that does the following.
make
java Main
I already tried to run "make run" but I get an error.
No rule to make target 'Main', needed by 'run'.
This is my Makefile.
JFLAGS = -g
JC = javac
JVM= java
FILE=
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
Main.java \
Class1.java \
Class2.java \
Class3.java \
Class4.java
MAIN = Main
default: classes
classes: $(CLASSES:.java=.class)
run: $(MAIN).class
$(JVM) $(MAIN)
clean:
$(RM) *.class
You need to add classes rather than Main.class as a dependency for run, since you haven't defined a rule for Main.class, i.e. this should work:
run: classes
$(JVM) $(MAIN)
I need to make a makefile to compile a couple of my java files to turn in but I have no idea how to do that. I searched the web with no luck. Currently my file is
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
Proj411.java \
ExecTime.java \
Clocks.java \
Instruction.java \
simulator: Proj411.java
Proj411.java: ExecTime.java Clocks.java Instruction.java
clean:
$(RM) *.java
I want to make sure this is doing what I want it to do. Proj411 is where my main is located and it is dependent on the three other files to run.
Also on a side note, any explanation on what a makefile actually does may help. Thanks
Java projects are usualy built with either Ant- or Maven- skripts, and are a bit more comfortable than the Makefile (-horror).
So try to move to ant, or maven. Ant may be a bit simpler.
I think you have to add the following line to your makefile:
classes: $(CLASSES:.java=.class)
Also, if you want to be able to just type "make" at the command line instead of "make simulator" you should add:
all: simulator
I have a problem with a makefile. I have three classes. They do pretty simple stuff. One does an addition on a subtraction, and the last one will instantiate the two and simply print the resulted addition and subtraction.
Now when I create my makefile, I compile my Plus.java and my Minus.java but don't know how to compile the main class because it depends on the previous two. I want to compile and run it from the makefile if it's possible.
I get the above results when I try to run make:
javac -g Plus.class Minus.class
javac: invalid flag: Plus.class
Usage: javac <options> <source files>
use -help for a list of possible options
make: *** [Operation.class] Error 2
I don't know how to proceed; please forgive me if my question is to simple but I am new working with these stuff. I've searched many sites but with no answer.
Here is my Makefile:
JCC = javac
JCR = java
JFLAGS = -g
default: Operation.class
Plus.class: Plus.java
$(JCC) $(JFLAGS) Plus.java
Minus.class: Minus.java
$(JCC) $(JFLAGS) Minus.java
Operation.class: Operation.class
$(JCC) $(JFLAGS) Operation.class
run: Operation.class
$(JCR) Operation.class
clean:
$(RM) *.class
You need to pass the names of source files to javac:
javac -g Plus.java
A better option is possibly:
javac -g *.java
Very few people in the Java world invoke javac directly or use makefiles. Most people use an IDE when developing:
Eclipse: http://www.eclipse.org
Netbeans: http://www.netbeans.org
And then a more comprehensive build tool for final / automated builds:
Ant: http://ant.apache.org/
Maven: http://maven.apache.org/
Both of those could be invoked from make, or indeed invoke make themselves, in order to integrate with your existing build system.
EDIT:
It seems you have a makefile issue. See if this works:
JCC = javac
JCR = java
JFLAGS = -g
all:
[TAB]$(JCC) $(JFLAGS) *.java
run:
[TAB]$(JCR) Operation
clean:
[TAB]$(RM) *.class
Replace [TAB] with an actual tab character - as you probably know this is incredibly important in make!
you need to run javac -g Plus.java Minus.java (not .class)
Try this. Alas, managing Java class dependencies is a pain using make (been there done that in relatively large projects), so you should still seriously consider using Ant or some other appropriate tool.
JCC = javac
JCR = java
JFLAGS = -g
default: Operation.class
Plus.class: Plus.java
$(JCC) $(JFLAGS) Plus.java
Minus.class: Minus.java
$(JCC) $(JFLAGS) Minus.java
Operation.class: Operation.java Plus.class Minus.class
$(JCC) $(JFLAGS) Operation.java
run: Operation.class
$(JCR) Operation.class
clean: $(RM) *.class
I solved it. It was a problem with the packages. I edited my files in netbeans and remained:
package operations
to every one of them
so I got Operation to compile but now it didn't run
Silly problem....
JCC = javac
JCR = java
JFLAGS = -g
default: Operation.class Plus.class Minus.class
Plus.class: Plus.java
$(JCC) $(JFLAGS) Plus.java
Minus.class: Minus.java
$(JCC) $(JFLAGS) Minus.java
Operation.class: Operation.java Plus.java Minus.java
$(JCC) $(JFLAGS) Operation.java Plus.java Minus.java
run:
$(JCR) Operation
clean:
$(RM) *.class