views:

451

answers:

7

Is it possible to enter an infinite loop at compile time?

My program seems to enter an infinite loop when I attempt to compile it. I have a class with a class constructor that calls the method gameRun(). gameRun() calls itself at the end of its execution, but should have the appropriate checks to be able to break from it during run time... However when I attempt to compile the class, I actually seem to get an infinite loop then.

My understanding of compilation is that it does not actually execute the code... Which would mean it would be impossible to enter an infinite loop unless there is actually a serious bug in the compiler's source. Is this correct?

I am writing in Java, and am using BlueJ (a beginner's IDE which I am growing out of) as my IDE.

Thanks in advance.

.....................................

Thanks to you all for so many helpful responses. Just thought I'd post an update, as this seems to have perked some interest, and I am curious about it myself.

I have not done a whole lot with BlueJ or this error since I posted the original error, becuase I had taken the source files from the project, and was able to successfully compile and run them with eclipse. This suggests to me that it is a BlueJ (or related) problem. I have continued to work on this project with eclipse without any more problems of this nature. I will follow up with more detail on the problem when I am able to use the machine with the original project on it again. (Nothing should have been changed since)

.....................................

As an afterthought... Is there any way I can link this question to an account I have created and registered since this was posted? I can't find a way to do that, and it would make keeping track of this more convenient...

+6  A: 

Some languages do allow the compiler to enter an infinite loop. Java isn't one of those languages. :-)

Chris Jester-Young
I'll try to figure out what it is doing, and why, then.Thanks a lot. =)
Jonathan
Just out of interest, which languages do, and what's the easiest way to make them do it?
deworde
C++ lets you do that if you use template metaprogramming - it's Turing complete, so it's trivial to code an infinite loop.
Pavel Minaev
@deworde: you can get C++ compilers to fail by recursing templates too deeply... dunno if that counts as an "infinite loop" though.
rmeador
C++ can probably have infinite compiler loops due to template recursion. Maybe C with recursive macros, but that's probably disallowed. Lisp alikes, and probably many other functional languages as well for the same reason, due to metaprogramming.
Steve314
If you code an annotation processor that contains an infinite loop and add this to your compiler classpath, javac could loop forever too :)
Jörn Horstmann
Also, some Haskell type system augmentations may allow an infinite loop at compile time.
Jeremy Powell
Excellent, thanks! Especially Pavel and Steve.
deworde
@Jorn - aren't you going to recurse until you run out of stack ? Which is different from an infinite loop.
Brian Agnew
+2  A: 

Does your compilation hang (loop) if you just use javac ?

I've never seen a compilation hang indefinitely whilst compiling Java and I'm wondering if BlueJ is having a problem instead.

Brian Agnew
The problem might very well lie with BlueJ. I was merely compiling the code, but it would never appear to finish compiling, and a window would pop up with a "stack overflow" error. It appeared that there were too many calls that the system was trying to keep track of or something... =/
Jonathan
That sounds most likely to me. Most 'solutions' here would result in a stack overflow, I believe. Glad you're getting to the bottom of it, though....
Brian Agnew
+4  A: 
erickson
BlueJ allows you to select any JVM, it's just about always sun's.
Bill K
+1  A: 

If BlueJ does use its own compiler, you may have found a bug in it, or in BlueJ's build tools that surround it.

You might take a BinaryChop approach to this one: break your program into pieces, compile them individually, and see if the compiler-hanging behavior can be isolated to a small, specific testcase. At the end of the day, you'll either have an excellent bug report to show the BlueJ people, or you'll find that your program actually does compile (yet you'll be scratching your head).

jleedev
I might try this sometime. In the meantime, I had switched to Eclipse over the weekend and have made significant progress there. (The same code compiles fine with Eclipse, I took the same source files from the folder to start with). If I did this I would only be doing it in an attempt to try to find if this is a BlueJ bug, but I do not think it is particularly important considering that BlueJ is made to be a beginners start to Java, and I can't really imagine someone in a high school course (like this was designed for) running code like this.
Jonathan
+1  A: 

AFAIK, Standard Java cannot be compiled infinitely.

Are you sure that the problem is at compilation rather than at some other feature that BlueJ provides? Many Eclipse-based IDEs perform multiple actions during a rebuild, and that compilation is just one of them. It is possible that something else does.

What exactly do you see? An unending Eclipse task?

Uri
I am given a "stack overflow" error and it shows a large list of some sorts of calls by what I would assume to be BlueJ. I am not on the system with the program on it right now, so I cannot put up the exact error, but it was not the beginner-friendly error messages I am used to seeing with BlueJ.
Jonathan
A: 

It would be theoretically possible to do if, for example, to compile a file, the compiler first had to have finished compiling that file. (Ahhh... Recursion). But I'd imagine checking for that kind of madness would be the first thing a real-world compiler would handle.

But I wouldn't think it would happen on a method/function, unless (postulating) the compiler was trying to resolve tail-recursion to an simpler implementation, and failing. But, again I can't imagine that would be an issue with a modern Java compiler, even if it exists at all. Certainly I'd imagine that the compiler would eventually give up and post an error rather than infinite looping.

It's far more likely to be the IDE than the compiler. At a guess, the IDE might be trying to trace a warning/error to its source in the code in order to highlight it and getting trapped. Does BlueJ have text-highlighting on compiler errors/warnings? You could try turning it off.

Although, as many others have already suggested, the first real test is to compile from the command line using

javac *.java

Or whatever files your code uses.

EDIT: Sorry I never got back to you. For future reference, to compile from the command line (I'm assuming Windows as your OS):

  1. Open the command line by going to the start menu and select Run...
  2. Type cmd into the Run dialogue, and click OK.
  3. This should bring up a cmd.exe console.
  4. From here, use the "cd" command to change directory to the directory containing your java files. (cd "My Documents\Java\Monster Battle\core")
  5. Once you're in the right directory, type "javac *.java". This will run the compiler without needing to deal with the IDE. It should pause while compiling, and when it's done, you get the command prompt back.
  6. If you type "javac *.java -verbose" you get full output, in case you get your infinite loop.

If that works fine, it's an IDE bug. Send them a bug report. If it doesn't, congratulations! You've found something really interesting, that will probably tie up some poor Sun developer for a month.

deworde
I seem to remember it giving an error with a stack overflow after some period of time, but I think it may have left the compiler running or something, otherwise I wouldn't have considered it an infinite loop... I will check and see exactly what it was once I am able. Any chance you can send me a bit more detail on how to do the command line deal (javac *.java)? I am unfamiliar with that, I have not used windows command line a whole lot, as most of my more significant projects in the past were remotely run on a linux machine, and were in C++. =s
Jonathan
A: 

Try to make the source code you are compiling as small as possible and still exhibit the behaviour you describe. The process of doing so, may help you identify what the problem is.

Thorbjørn Ravn Andersen