tags:

views:

102

answers:

4

I have make (Windows SDK), nmake (Visual Studio) and make (CodeGear)

Which version of make do I use to compile Java apps? Where do I get this "make" from? I downloaded the JDK and there is no make. I searched around for something to compile a Java makefile.

What do you use, if you have a MAKEFILE and some *.java files?

+8  A: 

Java projects rarely use make and makefiles. In fact, I've only ever seen this used once in practice however and that was 13 years ago, predating Java build tools. Instead Java projects use one of the two leading build tools.

Based on your question I'm going to assume you have no particular reason to use make (in that you're not trying to compile an existing project that only has a Makefile).

Ant

Ant is an XML-based scripting build tool that bears a little (but not a lot) similarity to a makefile in concept. An Ant build script will define a number of tasks. Those tasks will depend on other tasks. The tasks can include compiling Java source files, building jars and anything you like really.

Ant is very flexible but also you end up writing a bunch of boilerplate over and over (between projects). No two moderately complex Ant projects are the same and dependencies can become a huge problem. By "dependencies" I'm not talking about source dependencies but dependencies on third-party libraries. Ant has another tool called Ivy for managing dependencies.

Maven

Maven is the other common build tool. Maven differs greatly from Ant in that Maven defines project types (archetypes) such as a Web application. When you choose one (or more) archetype for your project, it mandates a directory structure that is quite deep. For example:

project-name
+- src/
   +- main/
      +- java/
         +- com/example/project-name/
            +- Java source files
   +- test/
      +- tends to mirror above
+- target/
   +- classes/
      +- com/example/project-name/
         +- Java class files
   +- test-classes/
      +- tends to mirror above
...etc...

Now some people don't like this, in part because some people just don't like being told what to do (imho) but also for things like legacy reasons and other factors. Personally I consider it a good thing because once you're familiar with any Maven Web application project, you can go to any other Maven Web application project and find your way around.

Maven will also give you lots of standard commands to run, such as compile, test, install, etc.

Maven is also useful for dependencies on third-party libraries. You declare these in your Maven project file and they are retrieved from repositories on the internet (and cached locally unless you set it up otherwise).

So my advice is use one of these two. You may be used to Makefiles if you come from a C/C++ background but it isn't really the Java way. Like I said, you may have reason to use make with Java such as an existing project but for any new project you should absolutely use Ant or Maven instead, in my opinion.

cletus
He's looking to make an executable, I think...
Rafe Kettler
You do if the project you're trying to compile comes with a Makefile.
Mark Peters
@Rafe - I don't see how you infer that. But anyway, he would need a special compiler to do that ... not a special build tool.
Stephen C
@Stephen: It's one option, anyway. And no, you can do it all from the JDK.
Rafe Kettler
Pardon? You cannot convince the Sun compilers to generate native code. You need a different (non-OpenJDK) compiler. The fact that that compiler *may* run in a Sun JDK is beside the point.
Stephen C
@Stephen C: I think Rafe is thinking executable Jar, which isn't an executable at all, really.
Mark Peters
@Mark - in that case, the OP can do that with a Makefile, Ant build file, Maven POM file, or by running the `jar` command at the command prompt. I still don't see any relevance to the choice of build tool.
Stephen C
I'm not sure why you would "assume you have no particular reason to use Make", given that in the question the user said "if you have a MAKEFILE", but downvote removed since it does detail the other options.
Mark Peters
Also, I can tell you first-hand that at least one company with Java products uses make to build them. It does make occasional sense when you're intermingling other things (like C++) or the product was migrated into Java, both of which were true in this case. Not a great choice for a new project though, you're right about that.
Mark Peters
hi, it's an open-source Java Package.
buttercup
+1  A: 

If you want an executable JAR, don't use the makefile, just follow these command prompts:

javac DiveLog.java
jar cvfm yourjarname.jar manifest allyourcompiledclasses.class

And to run the jar from console, you type:

java -jar yourjarname.jar
Rafe Kettler
+4  A: 

Use any version that processes your makefile. Make doesn't do any compilation, it's just a scripting system designed with building software in mind. It's just going to delegate to javac or something else for compiling your Java files.

I would try all three already on your system.

Mark Peters
does not work. CodeGear make chokes, NMake complains of invalid syntax, Windows make complains about / and \ path issues :(
buttercup
+3  A: 

There are two answers.

A1: If you want to use the MAKEFILE, you need to look at the build documentation that comes with the software to find out which version of make to use. If there is no build documentation relevant to your platform, look at the documentation for the platform the software was originally implemented on. There have been many different versions of Make over the years, and each version has its own quirks.

The other problem that you might encounter with the MAKEFILE is that makefiles are not portable. There are typically all sorts of platform dependencies in the rules that get run; e.g. command names, command paths, and so on. If you are trying to build on a different platform, these issues may require you to hack the MAKEFILE. (Aside: in the C / C++ world, there is a labyrinthine suite of GNU tools called automake / autoconf that is designed to address this problem. But by the sounds of it, you just have a vanilla makefile.)

In summary, if you want to stick with make, do what the build documentation says and in the absence of any relevant build documentation, just try any version of make and see what happens. And be prepared to learn "make-ese" and hack makefiles if there are platform related issues.

A2: IMO, a better alternative to this would be to ditch the existing MAKEFILE and create your own build file using either Ant or Maven.

Stephen C