0

I am experiencing a very annoying problem to me, imagine I compile the file FooBar.java:

$ javac FooBar.java

It compiles sucefully and then I run it as usual:

$ java FooBar < in.txt

I get the output as expected, the deal is, if I change the source code (FooBar.java) to print something else and compile it again I get the same output as the first time!

This is really making me mad... is there any cache for the javac? Even if I delete FooBar.class and recompile I get the output from the early source code. The only way I found to solve this is to delete FooBar.java and create a new file with a diferent name (I just copy-pasted the code), this works.

This bug (?) is really bugging me. I've searched for a solution or an explanation to why this happens but with no luck.

This seems to be related to my problem but not completely as I am not using any IDE and the Classpath points to my working folder: http://www.coderanch.com/t/519372/java/java/Executing-Old-Code

I would like to find out what's causing this as I don't want to make a new file everytime I update source code... I'm on Windows with Java 1.6.0_27

Thanks for reading...

5
  • How are you compiling? Are you using an IDE like Eclipse? Sorry didn't see the "i am not using IDE". What command are you using to compile? What happens if you delete you .class file? Commented Aug 16, 2012 at 0:56
  • As I said, I'm using javac. If I delete the .class file and recompile, the problem remains... it compiles the old code... Commented Aug 16, 2012 at 0:59
  • 1
    Delete the class file, don't compile and run the java command. This will verify that you running a different class file than the one you think you are. Commented Aug 16, 2012 at 1:02
  • 1
    Add -verbose to both the java and javac commands so that you can see more about what files are being created/accessed Commented Aug 16, 2012 at 1:09
  • Thanks, I've found out the problem. I was running the version from a .jar file that had the .class! That's why changing the name of the file and recompiling solved the problem. How do I close the question and mark it as solved? Commented Aug 16, 2012 at 1:30

2 Answers 2

1

Is there a package declaration? You may have put in a package declaration since you first compiled it. If you have package foo.bar the compiler will create the .class file in foo/bar/FooBar.class and you should run it with java foo.bar.FooBar. If there is no package it will create ./FooBar.class and you run it via java FooBar.

Sign up to request clarification or add additional context in comments.

2 Comments

No packages, it's a simple class with some array operations and a System.out.println() at the end. I just added another println() and the output didn't change
This might not have been the OP's problem, but it was mine! Thanks!
0

The problem won't be caching. Try this:

  1. look at the time stamp on your FooBar.class file (this is what is RUN)
  2. do your compile again javac...
  3. look at the time stamp again. Has it changed? if not, you are compiling to somewhere ese.
  4. The comments about the package by other posters would explain this. It would also be explained by having your class path set / changed

2 Comments

Thank you. The problem is explained by the fact that I was trying to compile a file with the same name of a file in a .jar which was already compiled. I came to this conclusion by deleting the .class file and running > java -v foobar I found that the .class being run was inside the .jar and not the one I had from changing the code. Problem Solved. :)
@andre.nunes So your classpath must have contained that jar file as well as your working folder, contrary to what is stated in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.