Eclipse classpath does not update changes in Project Properties - java

I am using Eclipse 3.4.1 Build M20080911-1700
I have tried to change the classpath directories for jar and the source directory using the Project Properties -> Java Build Path -> Libraries Tab or Source Tab. When I click OK button and then return to the properties page, my changes were not applied.
I have to resort in the work around by manually changing the eclipse ".classpath" project settings file.
Any ideas?

Is the .classpath file checked into source control? You may not be able to change it if you don't have it checked out.

Related

How to add referenced library JAR files using relative path for eclipse Java project in vscode

I checked this question and many other posts to help me resolve my issue, but nothing worked.
I trying to open an existing Java Project from Eclipse in vscode. The project was opened successfully in Eclipse 2021-06 and I fixed the referenced libraries by selecting the JAR libraries under lib/ folder and right-click - configure build batch - add select add to build path. Then the file .classpath was updated automatically. Then I had to download JDK 11 (needed for vscode) and JDK 1.8 needed for the Java project. I managed to work with eclipse to set the JDK correctly and all worked fine. I was able to compile the project, export the result JAR and deploy and all worked well.
I am having trouble to open this project in vscode using Open Java Project option. I am facing issues adding referenced libraries this Java project as it is a managed project (since it was created in Eclipse). When I try to add the libraries using vscode UI, I am getting mixed results and sometimes it is not working and the build fails. I was surprised as in some instances, things just work, and then they don't.
I tried to update .classpath file in vscode and still same issues. When I try to add the libraries from Referenced Libraries (Read Only) under JAVA PROJECTS tree in vscode by clicking +, it has no effect. This step will modify the file settings.json under .vscode folder by adding the relevant values to the property "java.project.referencedLibraries".
I am confused about how to configure the referenced libraries for Java projects in vscode. Following is a summary of questions:
How to add a library using a relative path to a Java Project based on the project root folder?
What is the difference between adding a library using the .classpath file and by modifying settings.json file? Which one will win?
How to add a folder with all JAR files to be as the referenced library to the Java Project? This is to avoid adding one file at a time.
Why the .classpath and .project files are not showing in vscode explorer view? It will only show when you open the file in vscode from the Windows File Explorer.
I found command Configure Classpath but it is read-only, which I think it is the same as the .classpath file. Is there a way to change it from vscode UI?
When I remove .classpath file or when I removed the classpathentry lines from the .classpath file, the node 'Referenced Libraries (Read Only)' under JAVA PROJECTS in vscode view was removed. Why?
See the snapshots below for more details.
I appreciate your help.
Basically speaking, java extension looks for jars from Referenced Libraries.
Here're my answers to your questions:
Right click the jar and choose copy relative path then add it to settings, click the refresh button then the added jar should be displayed under the option Referenced Libraries:
The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. When it comes to add jars, settings java.project.referencedLibraries wins.
No way to select
jar folders but you can use keyboard shortcuts to select all jars then add them.
There's a setting called "java.configuration.checkProjectSettingsExclusions", and it's true by default, so .project and .classpath won't be shown in VS Code.
The command Java: Configure Classpath works on my machine, which can customize current project.
My guess is when you open a new project, vscode popped up a window and ask you if trust it, and you chose Not, then project is read-only. Trusting the workspace then try the command Java: Configure Classpath again, it should be writeable.
The answer by #Molly Want-MSFT helped me a lot. Following is what I did to resolve the problem for good. I applied the steps below many time to verify they will work every time.
Steps to open a Java Project in both Eclipse and vscode:
Download the JDK needed for vscode, Eclipse and your Java Project.
Import the project in Eclipse and setup the JDK for the workspace and the project. Also, make sure to setup the compile level to match the JDK.
Set up the Project JDK to match the default of the workspace.
Build the project (Project-Cleanup) and Export the JAR to make sure all is OK.
Now open the project in vscode.
Setup java.home in Settings, in user and workspace sections. This must be JDK-11 or higher to allow vscode to function properly for Java Projects.
Add references to the installed JDKs in User's settings.json under "java.configuration.runtimes" section.
Restart vscode and take the option Configure Java Runtime from JAVA PROJECTS view. Make sure that the JDK of the Java Project is detected and working correctly.
You may have to open one of the Java Source Code Files. Wait a bit until it will settle down. Check the Java Build Status progress by clicking the spinning icon in the bottom right. This icon is for Language Server and it will turn into an icon that looks like thumbs-up when build/compile is done.
Close and open Configure Java Runtime to verify that the JDK was detected by vscode.
Ensure that both JRE System Libraries and Referenced Libraries under JAVA PROJECTS view are visible without any errors. Check the Problems view and try to resolve all errors.
The Referenced Libraries should be Read Only because this project was setup in Eclipse. You can delete the .classpath and .project files and open the project again in vscode, and try to fix the problems by adding libraries using the + button. When such files are deleted, the project will become Unmanaged. Later, you can restore such files.
When the project is unmanaged, you use Configure Java Runtime from JAVA PROJECTS view and you can check the project type. You can change the JDK to one of the installed ones as per the section "java.configuration.runtimes" in the user's setting.json.
If the the Referenced Libraries is read-only, it has no effect even if you can add libraries into settings.json, but the .classpath file will win.
From JAVA PROJECTS view, you can use the option Build Workspace and Clean Workspace to troubleshoot and try to resolve errors.
Finally, you can Export JAR from the option on JAVA PROJECTS view. This option is not clear and it looks like and arrow pointing to the right |-->.
I hope this helps, and if you have any question, please post a comment and I will try to answer back when possible.
I just change the path strings of the jars in the .classpath file and it works for me.

Android external Jar

I have some api with java and I need it in my android app.
When I am using it with including all packages and classes it works fine. Now I create JAR file from my java project using export. Now when I add the jar to my android app as a external jar my all errors are disappeared but when I run my app I got an error
java.lang.NoClassDefFoundError.
I have seen that kind of errors early in my apps and it was the problem of Jar. How can I generate JAR file to avoid this situation?
You have probably built that jar file with jdk 1.7. Classes compiled with jdk 1.7 are not included in apk file. Try to change java compiler to 1.6 before you export jar. In eclipse go to Window -> Preferences -> Java -> Compiler.
copy the jar file in the libs folder of your android project.
After that you may want to clean your project and rebuild it. You can even clear manually the bin dir content.
You have to add your jar file to your project's buildpath.
To make sure it's exported in your APK, you also have to check it under the Order and Export tab, in the build path preferences of your project.
A better approach might be the following:
remove the current exported jar from your project
select the project you want to import, right click -> properties -> android -> check 'mark as library'
right click your current project -> properties -> android -> locate the 'add' button in the bottom. Click it and select the library project.
Using this approach any changes in the library project will be reflected in your current project (at least after doing a clean projects).
I have done it !!! Thanks everybody.
So the solution is :
1. change java compiler to 1.6 before you export jar (As #Lecho mentioned)
2. Move the Jar file to lib folder
So it works !!! thanks everybody

accidentally deleted the default output folder in eclipse

I was rearranging some files in eclipse, and I accidentally deleted the default output folder. This is the location where eclipse stores the class files. Can anyone show how to recover it? Or rebuild it from the .java files?
EDIT:
I just restored the project from backup files. So this question is no longer relevant. Unless some future reader did not backup their application before making this same mistake.
from the menu bar select:
Project->Clean...
That will bring up a dialog box to select which projects to rebuild. The bin directory should be automatically regenerated.
Recompile your project, and Eclipse should create it again (default name is bin on some versions)
If not, navigate to your project settings, and go to
Java Build Path > Sourceand at the bottom you will see the default output folder. You can create it again by using the Browse button, and then Create new folder
All you need to do is re-compile your project, For that you can select your Project and do Ctrl+B or just select Project->Build Project
EDIT:
As you mention in the comments that you still do not see the class files. Please check the Default output folder by selecting your project Right-click Build Path -> Configure build path... then select Source tab and see what the Default output folder: mentioned there, your class files must be created there.

eclipse doesn't compile the project

I had running project opened in eclipse. After an accidental restart of windows, now when I open the eclipse I see my project is marked with a little red cross. Now when I run the main method I get a java.lang.NoClassDefFoundError.
I have tried restarting eclipse, Project -> Clean but it doesn't solve the problem.
When I checked the project directory, inside 'target' folder there are no compiled .class files. I tried building the project but I can't get the compiled class files, which is the reason for the error.
How do I solve this?
I tried all the solutions here, but here's what worked for me.
First, remove the project from eclipse.
Then, in your project folder, delete the 2 files that eclipse creates. They're hidden, but they're called .project and .classpath.
Finally, add the project back into eclipse and eclipse will recreate those 2 files and a new configuration for your project.
My specific issue was that it a project that was missing a Java Builder because it thought it was a scala project. The only way for me to reset this was by removing the project, deleting the 2 files, and adding it back in.
Maybe Eclipse's workspace files have become corrupted. Restart Eclipse and choose a new workspace folder (or choose Switch workspace from the menu). Then import the project files into a new project.
Try deleting the run configuration and remaking.
If that doesn't work, open the class, ctrl+f11 to run. That will force Eclipse to update its classpaths & rebuild its main indexes.
To delete the configuration:
- Menu -> Run -> Run Configurations.
- Select your configuration
- Press delete or use the delete icon
If you are using maven, this will likely fix it: Right click your project -> Maven -> Update project.. (or alt + F5), select your project, make sure "Refresh workspace resources from local filesystem" is checked and click ok.
My eclipse project has similar issue though the error appeared slightly different. I imported a third party java project into Eclipse, with Project > Build Automatically checked, do a Project > Clean..., no classes were generated in the output bin folder. Tried different options, no luck.
I noticed a red exclamation mark at the top left corner of the project name.
Right click on project name, Properties > Java Build path, I saw "Build path entry is missing: cci/src". Manually created src folder, the red exclamation mark gone. Do a Project > clean..., project compilation successful
P.S. The eclipse project is not properly organized, there are no code under src folder. I suspected src folder went missing while checked in by original author since there are no code there.
Was having the same problem and finally found the glorious hint here.
In my project there is one *.scala File and Eclipse Neon is incompatible with the Scala IDE, thus there is no builder for scala and the whole project is not built.
After removing the Scala file and the Scala Package from the linked Libraries )Project Properties --> Build Path --> Libraries) and afterward re-adding the Java Builder (no idea why this one went missing, but here's a useful help on how to add the Java builder when missing), the project could be built and run again.
Try refreshing and then cleaning the project.
For me the problem is in missing jars,the jars were added first and the location of those jars where changed after.
So When I correctly added all the required jars again to the build path, it starts to build project correctly.

Eclipse "Build Automatically" not cascading

I have a project in Eclipse (Indigo but similar in Helios) which has files in src/main/resources which get processed to generate *.java source files in target/generated-sources/xyz. The project settings have "Build Automatically" enabled so whenever these resource files are edited and saved the corresponding *.java files are generated. I've added target/generated-sources/xyz to the projects Build Paths. This all works perfectly.
The problem is that the changes to the generated *.java files are inconsistently rebuilt. If I have one of those files open in the editor, then it notices that it changed and asks if I want to reload it. Reloading the *.java files is enough for it to recognize that it has changed and trigger a rebuild. If I don't have it open then it doesn't automatically pick up the changes.
Any ideas on what I can do to let Eclipse (Indigo preferably) know that these generated files are being updated? Or better yet tell it to monitor those folders directly?
Thanks.
Try configuring Eclipse to automatically refresh. I'm not sure about Indigo, but in Helios, it's in Preferences → General → Workspace → Refresh Automatically.
Here it is in Indigo:
I assume you have added an "Program"-based builder to the project that compiles/translates the files into .java.
If you open the builder - via project properties -> Builders -> Edit... builder - then you can open the Refresh tab - se below.
Here you can then select which resources to refresh when this particular build is run... E.g. all resources in target/generated-sources/xyz...

Categories

Resources