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
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 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)
My makefile is below
Also, I would appreciate it if you told me how to move my .class files to ../bin/
JFLAGS = -cp
JAR = "RSBot*.jar"
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $(JAR) $*.java
CLASSES = \
src/Banker.java \
src/Eater.java \
src/Fighter.java \
src/grotgui.java \
src/InventTab.java \
src/Looter.java \
src/Potter.java \
src/W8babyGrotworm.java \
src/Walker.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
As you can see here, How to wildcard include JAR files when compiling?, you cannot use the wildcard '*' in the classpath to get several jar files unless you are using java 1.6 or above. Otherwise, you should write each concrete jar you need.
To put your .class files in the bin directory, you can use the -d <directory> option of javac to specify where to place generated class files.
just out of curiosity, why make? why not use a more modern tool like maven, ant or gradle?
They are designed for this sort of thing and usually give you what you want out of the box.
But to answer your question:
javac -d outputdir
For a homework assignment I have to make a makefile (physically and in software) for a series of .java files that I have written.
I have written up a make file:
JFLAGS = -d -g bin/
JC = javac
.SUFFXES: .java .class
CLASSES = \
cdn\communications\CommandReader.java \
cdn\communications\CommandReaderFactory.java \
cdn\communications\CommandReaderThread.java \
cdn\communications\DiscoveryCommandReader.java \
cdn\communications\Link.java \
cdn\communications\RefreshThread.java \
cdn\communications\RouterCommandReader.java \
cdn\node\Discovery.java \
cdn\node\Node.java \
cdn\node\Router.java \
cdn\utility\Utility.java \
cdn\wireformats\DeRegisterRequest.java \
cdn\wireformats\DeRegisterResponse.java \
cdn\wireformats\LinkInfo.java \
cdn\wireformats\LinkWeightUpdate.java \
cdn\wireformats\MessageType.java \
cdn\wireformats\PeerRouterList.java \
cdn\wireformats\RegisterRequest.java \
cdn\wireformats\RegisterResponse.java \
cdn\wireformats\RouterInfo.java \
cdn\wireformats\WireFormatFactory.java \
all : $(CLASSES)
clean : $(CLASSES:.java=.class)
But when I run it I get the message "make: Nothing to be done for `all'." and none of my files are made.
Is there something I'm missing here? I'm running the file from the directory that holds the "cdn" directory hierarchy?
Any thoughts would be appreciated.
You have not specified how to build the java classes in your Makefile. Essentially something like below...
.java.class:
$(JC) $(JFLAGS) $*.java
Refer to this link which has a good example.
Fix your all target to depend on .class files instead of .java files (that are already exist and thus " Nothing to be done").
all : $(CLASSES:.java=.class)
Also, you have to a add a rule to compile .java files into .class files:
%.class : %.java
$(JC) $(JFLAGS) $<
In case of using the rule above (so-called pattern rule), .SUFFXES: isn't needed anymore, you can remove it at all.
As outlined by others above, you can easily do something like this:
%.class : %.java
javac flags_go_here
... but there's a few problems with this:
If there are any dependencies between classes -- and there will be -- you'll have to get the order right. There aren't any tools or command-line options like what GCC provides for generating dependencies for you
Java allows circular dependencies between classes. It isn't possible to compile two classes that depend on each-other unless you compile them together, eg:
${JAVAC} ${FLAGS} class1.java class2.java
javac has a long start-up time. For small projects you won't notice it, but for any project where there's enough classes it'll add up fast, and your build times go downhill fast.
The best solution I've come up with is something like the following (using GNU-make style syntax):
all: my.jar
my.jar : c1.java c2.java ... cN.java
${JAVAC} ${JAVAC_FLAGS} ${^}
${JAR} cf ${JAR_FLAGS} ${#} -C src ${^:%.java=%.class}
# As a bonus, here's how you could do JNI stuff based on individual class files.
# This is necessary because nothing in the build actually depends directly on
# the .class files; if they don't exist and the .jar does, then you need some
# way to get the .class files without rebuilding the jar.
# It's written this way so it's re-usable.
# I'm using 'unzip' instead of 'jar' because it has the -j option so it won't
# dump some/irritating/path/to/work/with/when/all/I/need/is/the/.class/file
define CANNED_JAVAH_TARGET =
$(if $(filter %.java,${^}),,\
$(error When building ${#}: No jar dependency provided))
unzip -j -d /somewhere/to/put/temp/class/file \
$(filter %.jar,${^}) ${PKG_PATH}/${#F:%.h=%.class}
${JAVAH} ${JAVAH_FLAGS} <whatever flags are needed to make the header>
endef
${JNI_HEADER_TARGETS} : my.jar
${CANNED_JAVAH_TARGET}
There is a problem with the javah portion: for the JAVAH thing, if someone makes it depend on more than one jar, it'll break. There may be a few minor mistakes or things to work around in the above (going from memory here), but all in all it has worked for me without much hassle.