Make file for multi-directory project - java

I am trying to do a makefile for a java project that pretty much compiles the whole project and place the compiled files in a bin directory. This is my file architectures :
This is my makefile at the root directory :
BIN=./bin/
SOURCE=./src/
all:
cd src; make
clean:
rm -f $(BIN)*.class
and this is my makefile in the src directory :
BIN=../bin/
sourcefiles = \
MessageMondial.class \
Bonjour.class
classfiles = $(sourcefiles:.java=.class)
all: $(classfiles)
$(BIN)%.class: /src/%.java
javac -d $(BIN) -classpath . $<
if I go in the src directory and run the make all command everything gets compiled and is putted in the bin directory like it should.
However when I run the make all command from the root directory of the project I get this error :
How can I run the make all command from the root directory of the project?

This
sourcefiles = \
MessageMondial.class \
Bonjour.class
should be
sourcefiles = \
MessageMondial.java \
Bonjour.java
but I would also recommend you use maven, ant, gradle or sbt instead of make. Make was not a tool designed for building Java projects.

Related

How to run `jdeprscan` on an EAR

The jdeprscan tool determines lists all deprecated and non-existing dependencies. It can run on classes, directories and on a JAR.
But how to run it on an EAR ?
Inspired by https://stackoverflow.com/a/57217414/698168, I explode the EAR into JARs using the following script (Windows) :
rem remove previous run
rd /s /q ear
rem extract the EAR
"C:\Program Files\7-Zip\7z" x -oear *.ear
rem extract the WAR
cd ear
"C:\Program Files\7-Zip\7z" x -owar *.war
rem unify JAR from EAR and WAR
copy war\WEB-INF\lib\*.jar lib
rem make JAR with the classes
cd war\WEB-INF\classes
rem "C:\Program Files\7-Zip\7z" a -r my-app.jar
"C:\Program Files\Java\jdk-11\bin\jar" cvf my-app.jar -C . .
rem Note: using 7zip to create the JAR may lead to errors when running jdeprscan, thus we are using the jar command
copy my-app.jar ..\..\..
rem return to origin
cd ..\..\..
rem unpack all libraries...
cd lib
"C:\Program Files\7-Zip\7z" x -aoa -oclasses *.jar
rem .. and repack them as a fat JAR
cd classes
rem "C:\Program Files\7-Zip\7z" a -r 00lib.jar
"C:\Program Files\Java\jdk-11\bin\jar" cvf 00lib.jar -C . .
rem duplicate the fat JAR and make some cleaning
copy 00lib.jar ..\00lib.jar
copy 00lib.jar ..\01lib.jar
cd ..
rd /s /q classes
rem return to origin
cd ..\..
Note that this script does not use the librairies from the JEE Server (i.e. all the Maven librairies with scope "provided" will be reported as error: cannot find class by jdeprscan).
Then I generate a jdeprscan report using the following command :
"C:\Program Files\Java\jdk-11\bin\jdeprscan" --for-removal --verbose --class-path ear\lib\*.jar ear\my-app.jar > deprscan.log 2>&1
You can then inspect the jdeprscan.log file. The classes that are not found may not exist in the newest Java version (such as 11) or may be present in the JEE modules. A missing class looks like the following (BASE64Encoder is not provided anymore by Java 11 but is used by ChecksumHelper):
Processing class oracle/spatial/security/ChecksumHelper...
error: cannot find class sun/misc/BASE64Encoder
In the best case, you can find the JAR name above in the log file (e.g. Jar file my-lib-2.3.4.jar), otherwise you will need to determine the library from the class name.
Note: all the above was designed with the idea to migrate Java 8 to Java 11.

Beginner: Need to Run a Makefile in Windows

I am currently trying to install a program from Sourceforge. The installation process requires that I change directory paths in the makefile and execute it. I have been researching how to run the makefile but I am still lost and was hoping to get some help or be pointed in the right direction. I am running Windows 8.1. I believe the program is going to execute a java class file. However, I am unfamiliar with the language and not sure how I should alter the makefile to run in windows 8.1. I tried to run "make" in the command prompt but that didn't work. I posted the makefile below. I understand I should change the directory for ImageJ (which I have installed). But I'm not sure how I should alter path for windows and how I can then execute it. The manual instructs me to change IMAGEJ_DIR to the installation place of the program ImageJ. Then execute "make" and "make install" will generate the jar file. "My imageJ folder is located at C:\Program Files. Do I need to change the directory to execute this makefile, and if so how can I do that in windows? The makefile itself is located in my downloads folder. Any help would be greatly appreciated. Thanks!
JC=javac IMAGEJ_DIR=/home/wenja/ImageJ PLUGIN_DIR=$(IMAGEJ_DIR)/plugins/OpenBeamProfiler IJ_JAR=$(IMAGEJ_DIR)/ij.jar APACHEMATH_JAR=$(IMAGEJ_DIR)/plugins/commons-math3-3.2.jar LIBJAR=$(IJ_JAR):$(APACHEMATH_JAR) CLASSPATH=$(IMAGEJ_DIR):./:$(LIBJAR) CLASSDIR=./classes COMPILEFLAGS=
COMPILEFLAGS=-Xlint:deprecation
COMPILEFLAGS=-Xlint:unchecked
all: $(CLASSDIR)/BeamProfiler_Plugin.class $(CLASSDIR)/BeamProfilerFrame.class $(CLASSDIR)/BeamProfilerResultFrame.class $(CLASSDIR)/BeamViewWindow.class $(CLASSDIR)/BeamFunctionGauss.class $(CLASSDIR)/BeamFunctionSuperGauss.class
clean: rm -f $(CLASSDIR)/*.class rm -f *.jar
$(CLASSDIR)/BeamProfiler_Plugin.class: BeamProfiler_Plugin.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamProfiler_Plugin.java
$(CLASSDIR)/BeamProfilerFrame.class: BeamProfilerFrame.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamProfilerFrame.java
$(CLASSDIR)/BeamProfilerResultFrame.class: BeamProfilerResultFrame.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamProfilerResultFrame.java
$(CLASSDIR)/BeamViewWindow.class: BeamViewWindow.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamViewWindow.java
$(CLASSDIR)/BeamFunctionGauss.class: BeamFunctionGauss.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamFunctionGauss.java
$(CLASSDIR)/BeamFunctionSuperGauss.class: BeamFunctionSuperGauss.java $(JC) $(COMPILEFLAGS) -cp $(CLASSPATH) -d $(CLASSDIR) BeamFunctionSuperGauss.java
Note: the "_" in the jar file name in important to be recognized by ImageJ
for including manifest use
jar cfm Beam_Profiler.jar Manifest.txt BeamProfiler_Plugin.class BeamProfilerFrame.class BeamProfilerResultFrame.class
it is important to jar *.class to also get ...$1.class etc.
jar: all cd $(CLASSDIR) &&\ jar cf Beam_Profiler.jar * &&\ mv Beam_Profiler.jar ../
install: jar #if test ! -d $(PLUGIN_DIR); then mkdir $(PLUGIN_DIR); fi
cp Beam_Profiler.jar $(PLUGIN_DIR)
uninstall: rm -f $(PLUGIN_DIR)/Beam_Profiler.jar
Try this,
Open command prompt => Go to windows start menu (run programs) -> type the command "cmd.exe"; this will list the command prompt program in the list. Open the command prompt. Probably it'll be defaulted to your home directory. So issue the command "cd Downloads\directory_of_the_program_to_install" to navigate to the program directory. Now go to the real program folder, then find that make file, open it in edit mode, and then change the entry
IMAGEJ_DIR=c:\program files\imagej_directory. Then go to the command prompt that we opened before, and issue the command make and then makeinstall. This would do the trick.

Is there a way or tool to transform IntelliJ IDEA project to Makefile project?

I have classic IntelliJ IDEA project (no maven or gradle). I want to transform this project to Makefile project.
For example, I have console application without some frameworks. This application consists of Main.java, Class1.java and Class2.java. The source files are in src/com/site/ folder.
I want to write make in terminal and get Main.jar. Of course, I don't want to create Makefile manually.
I did not find any solution. Therefore, I use this snippet:
someproject -p out
javac src/com/example/*.java -d out/
cd out/ && jar cfe someproject.jar com.example.Main com/example/*.class && mv someproject.jar ../ && cd ..
echo "#!/bin/bash" > someproject
echo 'java -jar $${BASH_SOURCE[0]}.jar' >> someproject
chmod a+x someproject

How to make a proper Java makefile?

I need to make a makefile for a Java project.
My project is basic. A package which contains my main file and some others packages.
Can someone help me to make a proper makefile for that kind of project ?
Sorry I have not tested so it is likely to fail...
Assuming that what you need is to generate a executable jar file the following should work but I have not tested it.
The Makefile below assumes that your sources are located under ./src and that you are happy to use ./build for intermediary files (which is totally obliterated by the clean target so be careful).
Then 'make' or 'make jar' should generate the jar file.
NAME=MyProject
MAIN_CLASS=MyMainClass
SRC_DIR=./src
CLS_DIR=./build/classes
MANIFEST_FILE=./build/META-INF.MF
JAR_FILE=./$(NAME).jar
SRC_FILES=$(shell find $(SRC_DIR) -iname "*.java")
CLS_FILES=$(patsubst $(SRC_DIR)/%,$(CLS_DIR)/%,$(patsubst %.java,%.class,$(SRC_FILES)))
.PHONY: jar run clean mrproper
jar : $(JAR_FILE)
run : $(JAR_FILE)
java -jar $(JAR_FILE)
$(JAR_FILE) : $(MANIFEST_FILE) $(CLS_FILES)
jar cmf $< $# $(CLS_DIR)
$(MANIFEST_FILE) :
mkdir -p $(dir $#)
echo Main-Class: $(MAIN_CLASS) > $#
$(CLS_DIR) :
mkdir -p $(CLS_DIR)
$(CLS_DIR)/%.class : $(SRC_DIR)/%.java $(CLS_DIR)
javac -d $(CLS_DIR) -sourcepath $(SRC_DIR) $<
clean :
rm -Rf ./build
mrproper : clean
rm -f $(JAR_FILE)
Each time you execute Make it performs a find operation to get the list of source files, that might cause some delay depending on how many files and how fast is the file-system.... but you said is is a small project so it should not be an issue.
The double patsubst might well be compressed into a single one but I did it in two steps just in case.
Also notice that this solution compiles each Java class separately. This can be quite costly and it might be advisable to have another target to compile all at once ideally by creating a file that contains the name of all src java files and passing it to javac.

How can I make makefile for java with external jar file?

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.

Categories