Makefile to compile java in directories - java

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

Related

How to Create a Makefile to comile a C file and Java File Separately

I am new to creating makefiles with java files and c files together. I am trying to just have the two files compiled separately by using one makefile. The clean commands don't seem to be an issue, but the below code only compiles the C file.
What am I missing?
#target: dependencies
helloWorld: helloWord.c
g++ -std=c++0x helloWorld.c -o helloWorld
hi.class: hi.java
javac hi.java
clean:
rm helloWorld
rm hi.class
By default, it will only build the first target. Option 1, add an "all" before helloWorld like (also, there's a typo on your version of helloWorld).
all: helloWorld hi.class
helloWorld: helloWorld.c
g++ -std=c++0x helloWorld.c -o helloWorld
Or, you can explicitly run your existing target
make hi.class

convert windows javac command to linux

I'm having troubles with converting my javac command as used under windows (my libraries are placed within the lib folder, the source code within the src folder, and the compiled code should be placed within the bin folder):
javac -cp lib/\* -d bin src/\*.java
into the correct linux javac command. I have tried many variations including
javac -cp lib/* -d bin src/*.java
but that doesn't seem to work :
"invalid flag: lib/org.eclipse.paho.client.mqttv3-1.0.2.jar".
I am looking for a solution to this already quite some time, so any advice on this would be highly appreciated!
This is one of those odd things that happens when you don't put quotes around an argument.
Solution worked out in comments section: "lib/" instead of lib/
javac -cp "lib/*" -d bin src/*.java
The other issue was that ; is needed instead of : in Linux for classpaths.

Javac fails to find symbol when wildcard is used, but works when .java file is manually specified

When I compile with this compiler code:
#echo off
javac -d bin -sourcepath src/*.java src/sign/*.java src/Alert.java
pause
I don't get any errors.
but when i compiler with this code
#echo off
javac -d bin -sourcepath src/*.java src/sign/*.java
pause
I do get errors
the alert.java is the first file
Have you tried this one
Navigate to src directory
javac -d ../bin *.java sign/*.java
All the required jars and dependencies must be set in class path before compilation or you can use -classpath option while compiling.
--EDIT--
Try this one without -sourcepath option
javac -d bin src/*.java src/sign/*.java
The -sourcepath option expects a path, and you aren't giving it one.
When you do this
javac -d bin -sourcepath src/*.java src/sign/*.java
You are only picking up the files in src/sign/, since src/*.java is being evaluated as the source path.

Solution for error ClassDefNotFound in Java

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.

Compiling multiple packages using the command line in Java

Hi i have been using an IDE but now I need to run and compile from the command line.
The problem is that I have multiple packages and I have tried to find the answer but nothing has worked.
So I have
src/
Support/ (.java files)
Me/ (.java files)
Wrapers/ (.java files)
Do you know how to compile everything with javac?
This should do it (may require additional classpath elements via the -cp command line switch):
javac Support/*.java Me/*.java Wrapers/*.java
But if your build process gets more complex (and it will!), you should look into using Apache Ant for build automation.
You should use build tools like Maven or Ant for such tasks.
In the initial stages, when the project is not very complex you can use the following line to compile, with appropriate classpath in place(as suggested by #Michael):
javac Support/*.java Me/*.java Wrapers/*.java
javac -d compiled $(find src -name *.java)
If you really need to just use javac and standard UNIX commands you could to this:
find src -name \*.java -print0 | xargs -0 javac -d classes
The real answer is javac -d (places where classes to be built and placed) -sourcepath (source of the package at the root) -cp (classpath of the dependencies which can again be classes folder where the classes are build and kept) full qualified name of the java file.
Ex javac -d classes -sourcepath src -cp classes src\com\test\FirstSample.java
The FirstSample.java contains the main method. Pacjage structure mentioned below.
Before compiling
HomeApp
--src
------com\test\FirstSample.java (First Sample using the FirstPojo.java)
------com\test\FirstPojo.java
--classes
After compiling
HomeApp
--src
------com\test\FirstSample.java (FirstSample.java using the FirstPojo.java)
------com\test\FirstPojo.java
--classes
------com\test\FirstSample.class (FirstSample.class using the FirstPojo.class)
------com\test\FirstPojo.class
In many cases Ant is overkill. Just use a BAT file if you are in windows or a shell script (sh file) if you are in linux. You can create a text file which includes all your javac commands and just run that file when you want to build.
For example, I use the following bat file to build one of my apps:
#echo off
echo Building Shazaam...
del classes\com\aepryus\shazaam\*.* /q
del classes\com\aepryus\shazaam\engine\*.* /q
del classes\com\aepryus\shazaam\domain\*.* /q
del classes\com\aepryus\shazaam\persist\*.* /q
del classes\com\aepryus\shazaam\view\*.* /q
del classes\com\aepryus\shazaam\task\*.* /q
del classes\com\aepryus\shazaam\action\*.* /q
del classes\com\aepryus\shazaam\controller\*.* /q
javac src\com\aepryus\shazaam\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar; -d classes
javac src\com\aepryus\shazaam\engine\*.java -classpath \lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\domain\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\persist\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\view\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\task\*.java -classpath \lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\action\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
javac src\com\aepryus\shazaam\controller\*.java -classpath \lib\Servlet.jar;\lib\AepUtil.jar;\lib\AepXML.jar;\lib\AepRPC.jar;\lib\AepLoom.jar;\lib\AepHTML.jar;\lib\Sprout.jar;classes; -d classes
cd classes
jar cf ..\war\WEB-INF\lib\Shazaam.jar .
cd..
echo Complete
To compile Run below command [it will store all class files in classes folder]
javac -d classes Support/*.java Me/*.java Wrapers/*.java
**Note : classes folder should be created first
To Run java application, run below command
java -cp classes {mainfile_name}
Replace mainfile_name with your main file.

Categories