Java makefile run - java

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)

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

make: Nothing to be done for `default' JAVA

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!

Makefile Creation in Java

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?

*** multiple target patterns. Stop

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.

Makefile problems with java

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

Categories