Java compiler is using old version of source code

◇◆丶佛笑我妖孽 提交于 2019-12-12 05:13:25

问题


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...


回答1:


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:


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.



来源:https://stackoverflow.com/questions/11979245/java-compiler-is-using-old-version-of-source-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!