This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Mar 16, 2013 1:16 PM by 956015 RSS

Source code won't recompile

956015 Newbie
Currently Being Moderated
Hi,

I'm having an issue with one of my programs where if I make some changes to it and recompile the changes aren't reflected in my program. It appears that the class files aren't being modified for some reason. If I delete the class files and recompile, new class files are created and the changes are reflected in my program.
Other programs in the same folder don't have this problem. Even the program in question was fine earlier. I have older versions of this program in other folders and they don't have this problem either. Could there be something that I added in my code that would cause this problem? Or is it some other issue?

I'm using notepad++ to write my code and I am compiling and running it from the command prompt using javac and java commands.

Thank you,
Kevin.

I apologize I wasn't aware that cross posting wasn't allowed. Please delete this thread if necessary.

Edited by: 953012 on Mar 13, 2013 8:20 AM
  • 1. Re: Source code won't recompile
    PhHein Guru Moderator
    Currently Being Moderated
    Crossposted: http://www.java-forums.org/new-java/70116-source-code-wont-recompile.html
  • 2. Re: Source code won't recompile
    893253 Explorer
    Currently Being Moderated
    I have older versions of this program in other folders
    May be this be the catch because if any of those other folders are on classpath then .class file from those folders are executed instead of yours changed one.

    My plan of action in this case would be:

    1. check classpath and find which folders are on that path.
    2. Check timestamp of generated .class files to know which one is changed.
    3. Java Decompiler can also be used in this case to be sure.

    Edited by: J2EE_Life on Mar 13, 2013 8:26 AM
  • 3. Re: Source code won't recompile
    939520 Explorer
    Currently Being Moderated
    Perhaps you can insert a print statement in each file to determine which one is being compiled. For example, System.out.println("I'm in folder1/myDirectory/MyJavaClass.java").
  • 4. Re: Source code won't recompile
    956015 Newbie
    Currently Being Moderated
    Thank you for the suggestions.

    The code being executed appears to be the correct code. All my older versions have missing features so I can see it's not compiling those. I also have other programs in the same folder which don't have this problem.

    In addition I have since deleted all other versions of the program. I have also tried renaming the files but the problem persists. My only only guess is that there's something in the code that's causing this but I don't know if that's possible.
  • 5. Re: Source code won't recompile
    893253 Explorer
    Currently Being Moderated
    All my older versions have missing features so I can see it's not compiling those.
    How? Its not compiling but execution of program can actually show you the missing features.

    I have older versions of this program in other folders
    Well that shouldn't be the case. If you could paste the sample problem WE can assure that.
  • 6. Re: Source code won't recompile
    939520 Explorer
    Currently Being Moderated
    Perhaps if you post your compile/deployment command line statement(s) someone might see what the issue is.
  • 7. Re: Source code won't recompile
    956015 Newbie
    Currently Being Moderated
    Hi,

    I believe I've narrowed down my problem but I don't know what the cause is. I have a class that is my main program and in that I have:
              EventQueue.invokeLater(new Runnable() 
                   {
                        public void run()
                        {
                             JFrame frame = new AFrame();
                             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                             frame.setVisible(true);
                        }
                   });
    AFrame is a class i created that extends JFrame and in that I have:
    public class AFrame extends JFrame
    {
         public AFrame()
         {
              JPanel panel = new APanel();
              add(panel);
         }
    }
    I can compile this once. But if I make some changes to my code and try to recompile, the class files aren't being modified. However if I don't use the AFrame class and instead do this in my main program I no longer have that problem:
              EventQueue.invokeLater(new Runnable() 
                   {
                        public void run()
                        {
                             JFrame frame = new JFrame();
                                      JPanel panel = new APanel();
                                      frame.add(panel);
                             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                             frame.setVisible(true);
                        }
                   });
    Is there a reason why the former code is causing this problem? Thank you.

    Also regarding 936517's suggestion, I compile and run using the commands javac ProgramName.java and java ProgramName.

    Edited by: 953012 on Mar 13, 2013 9:55 AM
  • 8. Re: Source code won't recompile
    893253 Explorer
    Currently Being Moderated
    the class files aren't being modified.
    How can you say that?

    However if I don't use the APanel class.
    So there are different java class files in which APanel is used. In one case it works and other don't right?
    instead do this in my main program I no longer have that problem
    So you have some directory structure in which certain number of java classes have been packaged and proper import statements have been written.
    Now when you compile the class, you just have to find out where .class is created by comparing timestamp or using java decompiler.
    Now suppose if in your import statement you are referring to some directory structure where the new compiled .class is NOT stored, you will not be able to see your changes which might be case.
    Also you can check your import statement are referring to right java file.

    Also make sure you compile/run the program make sure you change to that directory containing your java file on command line and then do that because if you compile the java file from different directory which contains file with same name(as is your case), you will get what you dont want.
  • 9. Re: Source code won't recompile
    956015 Newbie
    Currently Being Moderated
    Hi J2EE,

    Thank you for your suggestions.
    I believe the class files aren't being modified because when I check the time stamps they are the same as when I first compiled. Also I meant to say if I do not use the AFrame class. The APanel class is another class that I defined but it does not appear to be the issue. I have three classes one of which is my main program, AFrame and APanel.

    If I create a Frame in my main program and add an APanel to it, there is no issue. If I instead I create an AFrame (which creates an APanel in its constructor) in my main program, then I have a problem.

    I don't have any packages or import statements. I'm fairly new to Java. I have all my files in a single folder. The process by which I compile and run my code is I first change to the correct directory and then I run javac and java commands.

    I have deleted all my other files of the same name and class. I have also renamed all the files in my program. I haven't done anything that I haven't done before regarding compiling and running but I've only started learning Java recently.

    Thank you for further help.
  • 10. Re: Source code won't recompile
    893253 Explorer
    Currently Being Moderated
    I'm fairly new to Java.
    You are welcome to Java :)
    I believe the class files aren't being modified because when I check the time stamps they are the same as when I first compiled.
    I have windows 7, on this if I click on the file,on General tab it shows *3 timestamps (Created, Modified and Accessed).* Whenever I recompile a java file then modified timestamp changes for .class file.
    Remember you have to check timestamp of .CLASS file and not .java file

    If I create a Frame in my main program and add an APanel to it, there is no issue. If I instead I create an AFrame (which creates an APanel in its constructor) in my main program, then I have a problem.
    So problem comes when you create an AFrame having APanel in constructor AND the problem you think is code change is not reflected during execution. But there could be a logical mistake as well.

    Here lets do a very basic exercise. Put a system.out.println("ANY TEXT U WANT") inside the constructor of AFrame in addition to other stuff like APanel and then coompile and run as usual.
    If the text is printed on console then you know that Code changes are reflected But you have other problem.
  • 11. Re: Source code won't recompile
    956015 Newbie
    Currently Being Moderated
    Hi J2EE,

    Thank you for the warm welcome and thank you for helping me with this issue.

    I did check the time stamp on the .class file not the java file and it was not modified after I try to recompile a second time, or anytime after the .class files are created.

    Right the problem appears to something with my AFrame class but I have no idea why that is. That is if I create an AFrame (which creates a APanel and adds it) in my main program the problem occurs. If I create a regular JFrame and add a APanel to it, there is no problem.

    I actually am writing a gui application so I didn't use a System.out.println() to test. The test I used was changing the background color of one of the buttons in my panel. If I change the background color and compile there is no change. If I delete the .class files and recompile the same code, I see that the background color has changed. Actually I can delete chunks of my code and it will still appear to compile but there is no change, the program runs as if it's working on old code.
    But there are no other files in the folder and no other files with the same name on my system.

    Edited by: 953012 on Mar 13, 2013 3:17 PM
  • 12. Re: Source code won't recompile
    893253 Explorer
    Currently Being Moderated
    That is if I create an AFrame (which creates a APanel and adds it) in my main program the problem occurs.
    I am not sure if this should be the case, but when you compile a java fille Do you compile also compile dependent files. For example, if there are 2 classes say A and B. A is my main program and inside A, I am creating class B.
    Now you made some changes in B and compiled it, then Compile A also. But this shouldn't be the case unless you make some structural changes. But I am not sure of your changes so I will say try it.
    I actually am writing a gui application so I didn't use a System.out.println() to test. The test I used was changing the background color of one of the buttons in my panel.
    In GUI also printing to console can be done. Your panel isn't working, So how can you rely on the background color changes? Print to console and see if changes are reflected.
    I did check the time stamp on the .class file not the java file and it was not modified after I try to recompile a second time, or anytime after the .class files are created.
    Just make dummy program and check does timestamp change for any of the program or not on recompilation.
  • 13. Re: Source code won't recompile
    handat Expert
    Currently Being Moderated
    Did you stop your program, ie was it running while you were trying to compile it? If it was running, then most likely your OS, ie windows, might have a file lock on that file preventing javac from updating it.
  • 14. Re: Source code won't recompile
    gimbal2 Guru
    Currently Being Moderated
    handat wrote:
    Did you stop your program, ie was it running while you were trying to compile it? If it was running, then most likely your OS, ie windows, might have a file lock on that file preventing javac from updating it.
    Actually in windows it is guaranteed. But then you get a compilation failure.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points