0

I was working on a project with a seriously large amount of classes that I want to compile to a jar. I know about entry-points and the manifest.txt and all the needed items inside my jar, my classes are all compiled and have the .class file and everything, but the problem is I will have to add all the class files to the final jar in compilation through a single line in Command Prompt. I was wondering and stumbled upon literally nothing in the internet if it could be done in an easier way because I will be updating my work constantly and have to recompile and re-jarify my work. I have heard of third party programs that will do the trick, but somebody on some website said that they could potentially be causing problems and stuff, so I dropped the idea quite quickly. Now that I am in a seriously tight spot though, I wish to hear opinions and suggestions on this. So to sum up:

  • I want a way to compile a big bunch of .class files in a single jar without typing all of them over and over again between compilations allowing me to save time and frustration.

  • I would prefer native stuff if this is even possible to do - e.g. the jar compiler of the JDK instead of anything third-party. If there is a way to do this using manifest or any other file in compile-time arguments, let me hear it.

  • Anyone who cares to suggest, discuss or give me a good reason why to or not to use third party applications for this will be most welcome.
  • Keep in mind that I work on Windows but my aplication will be cross-platfrom, so don't suggest as a main option some compile solution that will make a final file with a .exe extension (although if anyone knows how to do this, I would like to hear it in a comment as I wonder about this as well).

Thanks in advance and if you feel the need to ask me anything to help you reply, shoot away!

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75

3 Answers3

3

Have a look at this ant tutorial which shows how to write a simple build.xml which can compile and jar.

http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

You can then adapt it for your own needs.

Note: ant is only suited for smaller projects like yours.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Thanks a lot but I seem to have used a piece of code from http://stackoverflow.com/a/6849255/1650200 and some codes there have a warning about ant, will I have any problems whatsoever with this? – Angelos Chalaris Sep 30 '12 at 15:00
  • 1
    You _WILL_ need to understand enough to know what you are doing - you _MUST_ make an effort! For what you described ant will be fine. – Thorbjørn Ravn Andersen Sep 30 '12 at 15:04
  • The problem described at that question is only for some very special uses which I would be very surprised if you were using. – Thorbjørn Ravn Andersen Sep 30 '12 at 15:06
  • I am using this code to actually get the location of the file and name of jar for a couple of tasks. Will this give me any problems? – Angelos Chalaris Sep 30 '12 at 15:16
  • Not if you do not run it inside ant. – Thorbjørn Ravn Andersen Sep 30 '12 at 15:21
  • Which means? I have it inside a secondary file, not the one which is my entry-point file. So is this ok? (If you don't understand, ask away :) ) – Angelos Chalaris Sep 30 '12 at 15:24
  • Ant is used for _building_ your jar file. When you are done _building_ it, you will _run_ it using "java -jar foo.bar" - i.e. your own code. At that time there is no ant in play, and will not affect your code. Why do you write so complex stuff if you are unfamiliar with how classpaths work? – Thorbjørn Ravn Andersen Sep 30 '12 at 15:43
  • I am developing a project I came up with for a multitude of purposes and I am trying to pretty much beat myself to death in a way as you can see. :P No,seriously I am developing a tool for myself and some friends hopefully and by developing I mean trying really hard to try my hand at writing complex code with much functionality,reusability and learning a ton of packages and tools first-hand so my later development career will be easier and I will probably have enough experience to work on bigger projects. This is nothing too fancy by the way, but the code is lengthy and spread in many files... – Angelos Chalaris Sep 30 '12 at 16:03
  • ...and this is exactly why I am constantly scouring the internet for resources, ideas, solutions and tools and asking here whenever I don't find some stuff. And people like you pop up and give me a friendly and usefual/usable reply for most of my problems. even though I keep asking over and over stupid questions. But the best way to learn is to ask and that is what I am doing! As far as classpaths are concerned it's jsut another thing that I have minor knowledge on, but sooner or later I will learn quite enough as I did for example with observer and observable when someone else helped me! :) – Angelos Chalaris Sep 30 '12 at 16:06
  • 1
    It is my impression that classpath problems is typically the first _hard_ kind of problems that new Java programmers run into. I would suggest you spend some time with it. – Thorbjørn Ravn Andersen Oct 01 '12 at 10:20
  • Thanks for all the help and the support, I appreciate it quite a lot! :) – Angelos Chalaris Oct 01 '12 at 10:50
2

The solution to this, and related, issues, is to stop typing at the command line and use a build tool. The common tools here for Java builds are:

There are other less common ones. Both of these tools will provide you with what you need.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

Just want to add some information about Ant and Maven.

In your case, you need to automate the build of your application. The basic solution would be some kind of script but it's not used at all. Nicer solution exist :

If you come from the idea of a script to automate your build, you can use a tool like Ant, it's a bit like make and such tool in the C world where you define the needed tasks for your build in a configuration file. The problem with such solution is that it allow you to define your own structure for your build and a new comer to your project may have some difficulties to understand the logic of the build.

The other approach is to describe what kind of build you want to do, organize your sources and resources as it is done in most cases (by following a convention in fact). For example, java sources are in src/main/java, tests are in src/test/java, config files are in src/main/resources, and so on. In the description of your build you will just say : this is a java project and I want to build a War web application and execute my tests using jUnit 4. The dependencies of my project are apache xerces and hibernate 4. Then, the tool will know what to do without the need to say how to do. This is the way maven do.

In short, in the Ant approch, you will say how to do what you want and in the Maven approach you will define what you want to do and the tool will know how by default.

You may also be interested in some kind of hybrid approache like the one provided by tools like Gradle.

For more information :

Hope it helps

bouquetf
  • 435
  • 3
  • 13