Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a Java program that takes about an hour to run. While it is running, if I change the source code and recompile it, will this affect the above run?

share|improve this question
5  
No, unless you're loading classes dynamically it will not. –  Benjamin Gruenbaum Jun 14 '13 at 19:32
1  
Compiling code while it is running is not a very common use-case. –  user61852 Jun 14 '13 at 20:37

1 Answer 1

If the "while it is running" is a requirement, you need to be looking at dynamic loading of class files. Application servers support this type of structure - after all, it is how you do a redeployment of an application without stopping and starting the server.

You start getting things like How to dynamically reload classes when class files are changed in WebSphere Portlet Factory and designing the application correctly (see Dynamic Class Loading and Reloading in Java).

Dynamic loading of classes isn't the best idea - especially if done frequently. The reason is that the class itself is loaded into permgen in the vm (this depends on the jvm (see when do classes in jars enter the PermGen) - repeatedly reloading a class means you will be leaking the memory of the unused classes. (See Classloader leaks: the dreaded "java.lang.OutOfMemoryError: PermGen space" exception - a bit dated, but still useful).

This assumes you want to load it on the fly. If it is "I don't want to stop for an hour while I recompile" there are other solutions.

A build shouldn't take an hour to build if you change one class file.

In days of old, one used make to do partial compliation. Consider the

$ ls
Makefile    bar.c       foo.c
$ make all
cc -c bar.c
cc -c foo.c
$ make all
make: Nothing to be done for `all'.
$ touch bar.c
$ make all
cc -c bar.c
$ cat Makefile 
all: bar.o foo.o

bar.o: bar.c
    cc -c bar.c

foo.o: foo.c
    cc -c foo.c
$ 

It will only compile things that have changed. If you change one file, it only compiles one file.

So, only build the .class you need, or the .jar you need rather than the entire project. If it isn't organized that way, organize it so that you can just compile the parts you need.

This way, you won't need an hour to recompile everything - just a minute to recompile what you need.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.