Eclipse using wrong build while debugging - java

I am new to eclipse and java. The way I understand the directory structure is that you have source packages in src/ folder and they are built into the /build folder.
It was all working fine until recently. But today I tried debugging after creating a new class and it was throwing ClassNotFoundException when I do F11. On investigation, I found that it was using older versions of even the existing classes and not the new builds, and there was no .class file corresponding to my new class. I checked the build/ directory and it seems the builds are also old as it is showing yesterdays last modified time despite the fact that I have made changes today and tried to debug it. This problem is not limited to debugging as I exported the class into a jar file and it is still using yesterday's jar file. What could be the issue here? Is it not 'building' the classes? Have I messed up the default 'build' folder somewhere?

First you should check, that the "build automatically" feature is activated (menu "Project/Build automatically").
Next you should do a refresh of your whole project to tell eclipse to check for file changes that occured outside of the eclipse IDE.
If all that doesn't help, do a "Project/Clean.../clean all projects" inside eclipse.

I'd stop assuming and find out where Eclipse is putting your newer .class files. They can't be disappearing. Once you know that, compare it to the project set up you have and make sure that you know exactly where the .class files are going.

Related

IntelliJ only compiles files once

IntelliJ (Ultimate 2018.1) is not building my project properly. The project is using Maven which doesn't seem to have any problems (all libs are there). The problem is as follows:
Everytime I start up IntelliJ, I am able to build all changes exactly one time. I can change contents of my files and those changes will be contained in the build. But every change I do after that first build is ignored by the build tool. So, every time I build the project (ctrl+f9) after that, I get "All files are up-to-date" and nothing is compiled. (If I am running the app on the application server and try a hot swap, the build says "loaded classes are up to date ...")
Only a restart of the IDE lets me rebuild the project.
Edit: As I just found out, a restart of the IDE is not the only possibility to make a build possible again. In the state of not being able to compile, I changed a setting in the compiler settings. After that, I was able to build again. But only once. I then changed the setting back and well, I could build again. Looks like something odd in the IDE. /Edit
Edit2: Oddly enough, the explicit compiling of the class (ctrl+shift+f9) is working. So the problem circles around the compiling of the whole project. While this is making IntelliJ usable again, it's error prone regarding multiple changed files... /Edit2
A few notes and configurations of the project:
The build produces a .war
It is deployed on a wildfly (which is not started once in this cycle, so this shouldn't be the root of the problem)
The exact same project was formerly developed in Eclipse where building and Hot-Swap worked flawlessly (and still do when I try Eclipse again)
Maven Config:
Compiler config:
Check with Intellij version. As I am using 1.4 community edition and for me its running proper.
Even I have updated few dependencies after building First Time and it get's rebuild automatically.
So for my understanding what you can do it just check with the Intellij update or change the default directory and rebuild it.
The problem with the "Build Project" command is the source discovery of IntelliJ. A long time ago, we made the choice to place our sources inside a folder named ".git" (not the folder in the repository), so IntelliJ had problems to discover the code inside that directory. The reason for that was an old approach of Eclipse to clone repositories into a .git folder. The directory looked like this:
C:\dev\.git\workspace and inside that folder was another .git folder (from the repository).
So, the answer is:
Don't put your git repository in a folder named ".git" or IntelliJ will not compile it (unless you only compile class wise).

Eclipse not recognizing changes in code

A friend and I are working on a java project in Eclipse. Whenever we "ship" a new version we export it to a runnable jar.
It seems whenever we try to send the file back and forth (over google drive) to work on separate parts of it something gets messed up. This doesn't happen for him, but when I download the file from Google Drive, Eclipse does not recognize any change I make to the src.
It doesn't matter how many times I change the workspace, move/ rename the file, clean/ build the project. Nothing seems to be solving this problem. Any advice on why this is happening and how to fix it?
It would be infinitely better for the both of you if you looked into some kind of source control system, as opposed to a file sent through a file sharing service.
Sharing the source code in a JAR doesn't allow you to:
Keep a revision of known working code, in case something breaks
Reliably import the code
Keep track of changes (who did what to what file, etc)
Look into something that's suitable for sharing between users. GitHub and BitBucket both offer free Git hosting, which is a very popular source versioning tool.
Eclipse is probably building the new version of the runnable JAR in the project's output directory, which by default is something like %PROJECTDIR%/target. The new version of the JAR with your changes will be there, and you'll have to export them again to trade files back again. See also: How do I change a Java project's bin folder in Eclipse?
it is probably because it is not updating yours .class, try delete .class of "target" folder of your project, make clean (project - clean) and save again the changes
There are many steps where you can solved it,
Clean Project and Re-Build
Remove .metaData and .settings from Workspace
Clear cache
In my case, I have tried much more and I got to resolve it by removing Classes, test-classes folders if Its there in Target folder

How to run a modified java program in eclipse?

I have a project in Java which get a file using smb client. When I edit this code and run it again in Eclipse, it is not running my modified code but running the same old one.
Checklist:
I saved the file and made sure there are no write errors.
Build Automatically is enabled.
Run configuration is configured properly.
There are no unresolved errors.
what am i doing wrong?
To find out whether Eclipse has really compiled your code, have a look into the bin/ folder of the project and check the date of the .class files in there. You will have to do this outside of Eclipse since it hides the content of this folder by default.
Use "Clean Project" to force a rebuild.
If that looks correct, the next step is to go to the Debug Perspective. Open the Properties Dialog for the process. Eclipse will then show you the classpath that it used to start the process. Make sure the paths are correct.
Delete bin folder data and clean project. To clean project:
Go on Project-->Clean-->select your project-->clean-->build project
This will solve your problem, if this does not work, please write a Hello world program and check this works or not. If this is also not working, check your Eclipse

How to change workspace directory when upgrading from Eclipse 32 to 64

Oops. I am admittedly new to Eclipse. I recently upgraded to Win7-64 from XP-32 (old Dell dual-processor Work station). So thought I could simply install Eclipse-64, install all plugins. and redirect Eclipse-64 to point to new workspace directory containing the old project files. Changing path is easy enough, but Eclipse package/project explorer sees nothing in my new directory. Should I cut paste all existing files and put in default directory then import to newly created directory. Should I 'package' the old files and use as class library? Should I re-install Eclipse-32 and start over? Any expert advice here would be greatly appreciated. I have read all posts regarding this issue, but believe there may me a 32/64 issue here. Hope I'm wrong and transition will be smooth. I don't want to lose access to all my 'code in progress' or go back to Eclipse-32. Thanks in advance!
Go to File -> Switch Workspace -> Other. Follow the prompts.
The Eclipse UI is complex.
I had new directory path correctly assigned in Eclipse-64. But had empty project explorer view. So cut and pasted old project folders from newly created path to default installation workspace for E64. Then 'import' message said I needed to create project folder in order to do that. No problem. Then I imported all folders from old Eclipse version to new directory path and wallah! Opened bin class file and all code is there! I may have screwed up in overwriting version info and other E32 files over existing E64 files here in new install workspace folder, but at least I know how to do this now. So thinking of re-installing E64 and 'all' plugins (a real pain), but at least I will know I am new-platform compatible and all will be good in my world. As always, thanks for expert advice here. The 'full' stack rules!

Re-building a deleted class file in Eclipse

I accidentally deleted a .class (Java bytecode) file in my project (on the filesystem, not using Eclipse itself). Easy to fix, right? Just re-build it. But that doesn't work! Even if I select "Build Project" or "Build All" or "Build Automatically" from the "Project" menu, nothing actually happens on the file system, and I still get:
Exception in thread "main" java.lang.NoClassDefFoundError
I just want to re-compile this from the source code I already have!
By the way, when I choose "Clean..." from the "Project" menu, Eclipse doesn't delete any files either. I have also tried re-importing the project into a different folder, but Eclipse just copies all the .class files and the problem persists.
The OP answered his own question in the comments (2 and a half years ago):
Found the solution: another project on which that project depended could not be compiled, because it could not be cleaned, because Eclipse wanted to delete the .svn directories throughout that project (I have no idea why), and it could not because some of the files didn't have write permission. I was happy to wipe out all the .svn data just to get this working! Thanks for the hint. – user690075 Sep 7 '11 at 1:25
In regards to the bounty
This question has not received enough attention.
This problem keeps wasting hours of my time.
IF the OP's answer didn't resolve your issue, you should ask a more specific question on a new post, describing what you've attempted and how the OP's solution didn't resolve your specific issue.
That being said, assuming you did try the solution the OP posted, it is possible a different issue (that wasn't caused by deleting a class file) is causing the same error. Because you started a bounty on someone else's question and you can't get your prestige back I thought it would be appropriate to mention it might be worth your time to make sure your JDK version(s) are compatible between old or external source code used in your project. You'll get the same error NoClassDefFoundError when the compiler reaches a point in your code that references an object/class that's defined in a library that was developed on an incompatible JDK, it's missing key internal dependencies that are not found within your JDK version.
I would go into more detail, but since this question is specifically about an error that came about from deleting a class file I don't feel it's right to do so.
Do a complete clean
1) Find and delete the .eclipse folder (you may back them up first)
2) Delete related .class files
3) If there are any .svn folders, delete them either manually or via your svn client
4) Do not use auto build for this, but manually select only the broken project and do a clean (in case there are dependencies)
If that fails, probably a good idea to package your source codes and re-import as a new project. That can avoid wasting time on a probable IDE bug
In more traditional languages, programs are loaded all at once as part of the startup process. Java doesn’t have this problem because it takes a different approach to loading. This is one of the activities that become easier, because everything in Java is an object. Remember that the compiled code for each class exists in its own separate file. That file isn’t loaded until the code is needed. In general, you can say that “class code is loaded at the point of first use.” This is usually when the first object of that class is constructed, but loading also occurs when a static field or static method is accessed.
If You can't restore from local history. Then you are out of Luck. Use Source Control Management Tools like SVN or Git to avoid such surprises next time.
If you are having source file try to compile file along with dependencies alone in console or other IDE and copy that class file let the errors be errors now edit source file in eclipse try to build again. Hopefully this will not work because even eclipse will neglect Re-compiling some files while building Project. Better Give a Try.
You sure this source file is in your project's source set? Because Eclipse will only compile and put it in your classpath in that case. Right click the project in Package Explorer, Properties -> Java Build Path -> Source. The enclosing folder should be there or Eclipse won't compile it.
In case, say, this source file of yours was once in source set and was compiled that could explain why it was working up until you removed the binary.
In order for this problem not to happen I suggest having Scrub output folders when cleaning projects being selected in Java -> Compiler -> Building and Build automatically... on in Project menu.
Also make sure your project compilation/build succeeds, otherwise Eclipse may not compile all the classes.
If it still doesn't help it could be important what type of project you are having problems with: Java Project, Maven Project, Gradle Project, etc.
To the person that put the bount out, maybe you could just commit all your changes to what ever code repository you have, after exiting eclispe just delete the whole workspace, then create a new workspace and re import all the files into the new workspace from your code repository.

Categories