Stingi
Stingi

Reputation: 48

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!

Upvotes: 3

Views: 1619

Answers (1)

seenukarthi
seenukarthi

Reputation: 8674

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?

Upvotes: 1

Related Questions