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
Related
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!
I would like to create makefile that will compile java files which are in specific directory. Here is what I've got:
JCC = javac
JCR = java
JFLAGS =
-source 1.6 \
-target 1.6 \
-d out
main: folder all
all:
$(JCC) $(JFLAGS) src/com/company/*.java
folder:
mkdir out
But once I use make command in my main directory it says:
make: *** Missing objects. Stop.
Can someone point out what I'm doing wrong?
Thanks in advance for help.
--UPDATE--
I've tried to do:
main:
mkdir out
javac -source 1.6 -target 1.6 src/com/company/*.java -d out
But still:
"No rule to make target 'mkdir', needed by 'main'. Stop."
---SOLUTION---
Don't write makefiles in gedit. It makes wrong tabulators. Use nano or sth else.
I'm not good at writing makefiles, but I recently used something like this, maybe it will be enough for you to modify for your needs, first it compiles the code and then it packages it to jar:
all:
mkdir out
javac -source 1.6 -target 1.6 src/main/java/com/my/package/*.java -d out
jar cfm My.jar META-INF/MANIFEST.MF out/com/my/package/*.class
I have five class files Servant.class, Server.class, Client.class, TransferRequest.class and TransferResponse.class. My Makefile is at the below. I have this error for any of my class file:
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Makefile:11: recipe for target 'TransferRequest.class' failed
How can I cope with this error? I really tried all solutions which was written here such changing path or sth. This makefile is also 5th or 6 th one. The other well known makefiles gave same error too. I am on Windows machine I use cygwin.
Makefile:
JAVAC=javac
sources = $(wildcard *.java)
classes = $(sources:.java=.class)
all: $(classes)
clean :
rm -f *.class
%.class : %.java
$(JAVAC) $<
Add a classpath (with the -cp) option to your javac.
JAVAC=javac -cp "$CLASSPATH"
Or you could change
$(JAVAC) $<
to add the classpath
$(JAVAC) -cp "$CLASSPATH" $<
This is a problem with javac. Either something is missing from your make file or your java installation is broken.
Find out if you can compile a file by hand with javac to narrow it down.
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