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?
Related
And also so that it puts the .class files in a ClassDir directory? So far, I've only been able to make it work when everything is in same directory.
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
# This uses the line continuation character (\) for readability
CLASSES = \
Context.java \
ClosedState.java \
OpenState.java \
Floor.java \
State.java \
Elevator.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
I have a java project, and need to write makefile to let it run on Linux. My project includes external jar files and resource package(.txt resourses).I am really a newbie for Linux, and just learn how to write makefile.
I refer to some materials and write a makefile like this:
# Set the file name of your jar package:
JAR_PKG = ADBproject1.jar
# Set your entry point of your java app:
ENTRY_POINT = adb/Bing_WebResults/Run.java
# Need resource directory
RES_DIR = yes
SOURCE_FILES = \
adb/jsonModels/Metadata.java \
adb/jsonModels/Result.java \
adb/jsonModels/Data.java \
adb/jsonModels/DataContainer.java \
adb/models/Weight_ID.java \
adb/models/Pair.java \
adb/models/Document.java \
adb/models/Collections.java \
adb/Bing_WebResults/Bing_Search.java\
adb/Bing_WebResults/Run.java \
JAVAC = javac
JFLAGS = -encoding UTF-8
vpath %.class bin
vpath %.java src
# show help message by default
Default:
#echo "make new: new project, create src, bin, res dirs."
#echo "make build: build project."
#echo "make clean: clear classes generated."
#echo "make rebuild: rebuild project."
#echo "make run: run your app."
#echo "make jar: package your project into a executable jar."
build: $(SOURCE_FILES:.java=.class)
# pattern rule
%.class: %.java
$(JAVAC) -cp bin -d bin $(JFLAGS) $<
rebuild: clean build
.PHONY: new clean run jar
new:
ifeq ($(RES_DIR),yes)
mkdir -pv src bin res
else
mkdir -pv src bin
endif
clean:
rm -frv bin/*
run:
java -cp bin $(ENTRY_POINT)
jar:
ifeq ($(RES_DIR),yes)
jar cvfe $(JAR_PKG) $(ENTRY_POINT) -C bin . res
else
jar cvfe $(JAR_PKG) $(ENTRY_POINT) -C bin .
endif
But I don't know how to add those two external .jar files (gson.jar, commons.jar) into makefile. And I'm not quite sure, whether the file paths I wrote are correct.
javac has a -cp and -classpath argument:
-classpath <path> Specify where to find user class files and
annotation processors
-cp <path> Specify where to find user class files and
annotation processors
They seem to be equivalent as far as the documentation is concerned.
I solve the problem by adding all *.jar files to a new folder "lib".
Then
javac -sourcepath src/ -classpath lib/*.jar
will solve the external jar file problem.
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
I would like to create a runnable jar in the same manner in which eclipse does, but from CLI. I am using buildbot and I would like to be able to automatically create, for example, nightly builds automatically, but are also runnable.
My Makefile:
JFLAGS = -g
JC = javac
JAR = jar
JARFLAGS = cfe ./bin/java.jar alone.Gameloop -C resources . -C ./
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $(wildcard alone/*.java)
#$(JC) $(JFLAGS) $*.java
CLASSES = \
$(wildcard alone/*.java) \
# alone/Enter.java \
# alone/GameLoop.java \
# alone/ImageRender.java \
# Blah.java \
# Library.java \
# Main.java
all: classes
default: classes
packages: jars
jars:
#echo $(JAR) $(JARFLAGS) $(CLASSES:.java=.class)
#false
$(JAR) $(JARFLAGS) $(CLASSES:.java=.class)
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
Thanks for the help! :)
Your stuff looks okay all you have to do is add $(CLASSES) to the jars target as a dependency:
jars: classes
$(JAR) $(JARFLAGS) $(CLASSES:.java=.class)
and then make jars will do the stuff nightly
I need to make a makefile that compiles and executes my classes with an external jar file..
I have 4 classes;sync.java, FileSynchroniser.java, DirectoryTracer.java and SyncFileTracer.java.
I also need to include gson-1.7.1.jar ..
Heres my make file
.SUFFIXES: .class .java
JAVAC= $(JAVAHOME)\bin\javac
PATH=$(JAVAHOME)\bin;$(PATH)
CLASSPATH=.;$(JAVAHOME)\lib\classes.zip;$(JSDKHOME)\lib\classes.zip
DEST=.
DOC=.
JAVA=$(JAVAHOME)\bin\java
JAVACFLAGS=-deprecation
.SUFFIXES: .java .class
.java.class:
$(JAVAC) -classpath $(CLASSPATH) $(JAVACFLAGS) $<
CLASSFILES = sync.class \
FileSynchroniser.class \
DirectoryTracer.class \
SyncFileTracer.class
SOURCEFILES = sync.java \
FileSynchroniser.java \
DirectoryTracer.java \
SyncFileTracer.java
# begin ---- JAR support ----------
JARFILE= gson-1.7.1.jar
$(JARFILE): $(CLASSFILES) $(SOURCEFILES)
jar cfm0 $(JARFILE) <<manifest.tmp $(CLASSFILES)>>
# end ---- JAR support ----------
all : $(JARFILE) $(CLASSFILES) doc
doc : $(CLASSFILES)
javadoc -version -author -d $(DOC) $(SOURCEFILES)
install :
copy $CLASSESFILE $(DEST)
clean:
del $(CLASSFILES)
Thank you
Are you referring to making the makefile in a Linux OS?
Check this - make makefile guide
http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html
or this - example
http://www.cs.swarthmore.edu/~newhall/unixhelp/javamakefiles.html
hope it help.