Creating a Java Make File

EDIT: Essentially, I'm trying to do the same thing as typing "javac sim.java" and then "java sim (commands)". Is there a way to replace this with a makefile?

So, I looked through a lot of examples on the Internet and tried to change them to what I needed, but nothing worked. I need a make file so that my teacher can create my project by simply typing "make" in the terminal (for some reason this is a project requirement). My ultimate goal is to type β€œsim” and then the necessary commands defined by my code. The code works in eclipse, but I can not get it to work using these commands in the terminal. It will make a file, but it says "sim: command not found" when I try to enter "sim .... (arguments)" into the terminal. I am sure this is a stupid question, but we did not find out about it at school, and I have no experience with Makefile.

Below is my make file.

JFLAGS = -g JC = javac OPT = -O3 #OPT = -g WARN = -Wall sim: sim.class sim.class: sim.java $(JC) $(JFLAGS) sim.java clean: $(RM) sim.class clobber: $(RM) sim.class 
+5
source share
3 answers

Using make with java can be an exercise in screwdriving as soon as you have more than one class.

Working Java code will use packages. This way you will have a complex directory tree that reflects the structure of your package, with your .java source files. Writing rules that β€œsee” all of these files is a pain.

You must call javac in all source files at the same time to get the correct results. Thus, the usual template β€œrun one command to turn one source file into one compiled file” does not work so well.

However, it seems that your main problem at the moment is that you expect Java to create an executable; a file with a name like "sim". No, this will not happen in a simple way. The java compiler creates .class files; a whole tree of them for your source files. You can run them directly from them, or you can pack them into a JAR file.

To get all the way to something that looks like a simple command line executable, you need to use a more sophisticated tool that wraps it all with a script on the front panel.

Now you just need to do:

 java -cp . NAME_OF_THE_CLASS_IN_sim.java 

to run after the make file is completed.

+4
source

For a simple project without a lot of files or dependencies, I just use scripts.

To build:

 javac -cp .;* *.java 

For start:

 java -cp .;* SomeMainClass 

Replace . to any path (s) you need for your source. * Will use any jar along the default path or use a different path, such as lib/* .

+3
source

make executes the recipe, executing its dependencies, and then the recipe itself. Therefore, if you add a command that runs your compiled code to the top recipe, enter "make", also running the code after compiling it. I can’t believe how many people don’t know this!

Sort of:

 sim: sim.class java sim 

Will do what your mentor requires.

0
source

Source: https://habr.com/ru/post/1215822/


All Articles