.class file opens instead of .java while debugging - eclipse

Current setup:
MainProject which is a Library Project
BranchProject which is a new projects and has MainProject as a Reference
Whenever I debug and a file from MainProject is on focus (actually BranchProject has only graphic and xml layout changes) the Debug window opens a .class file which is read only. I want it to open the .java file so I can edit it directly.

Skyler's answer from this post worked for me:
Opening source code from debug view edits .class after Android R18 update
Here is a summary:
The fix is to right click the Project name in the debug view, and select "Edit Source Lookup..." from the menu. From there, remove the Default lookup path. After that, manually add the associated projects (not jars) that your project references. This is done by clicking Add, selecting Java Project, then checking the appropriate projects.

When you're using a Library project one of the things you're in fact doing is compiling your Library project into a jar and then referencing that jar in your calling Project.
If you right click the Project, and select "Configure Build Path" you'll see a tab called "Libraries", if you look inside "Android Dependencies" you'll notice a list of jar's corresponding to your Library projects.
These jars are expandable, showing you that they have a slot for a source attachment. Usually this would be editable allowing you to directly link the source but in terms of ADT these are already filled and are uneditable.
When debugging these files you're linked to a read-only class file with this attached source. This is because you're not running against source files directly, you're running against a pre-compiled class file. Until the ADT team get this functionality in place, you're pretty much forced to jump to the direct source code and rebuild everything.
EDIT
See #Steven linked answer :)

I faced the same issue while debugging the a .java file using Eclipse IDE. As per my understanding this issue comes when we put the xyz.class file of xyz.java file or JAR at the project build path. Delete the .class or JAR file from the project class path and rerun .java file in the debug mode. This time you see a source not found window. Click on "Source not found" button and check "Find duplicates..." at the bottom of the window. Done your problem is solved :)

The problem is that the class file is preferred over the java (by default), here is how you can change that for Eclipse (tested on NEON 2):
Right-click on the Project in the Project-Explorer, click Properties
On the new window select: Run/Debug Settings
Create a new configuration (or duplicate another one)
Select the new config and click Edit...
Go to the tab Source
Select the Default and Remove
Create a new path with Add..., select Java Library, then JRE System Library
Create a new path with Add..., select the location where the sourcecode is by Workspace folder (if it is a project in the same workspace) or File System directory (it it is not)

I think this depends on, how you set up the dependency in eclipse. You should set up your BranchProject to depend on the source-Files of your MainProject. If you depend on compiles Class-Files is obvious that the debugger opens the class files, because it does not know about the source files.

I found a good solution for me here:
Using Android library in eclipse and jumping to class files instead of source file that is within eclipse workspace
Simply, select each library project your project depends on, and use Top or Up to move it above the projects outputs. Eg. move all library projects to the top.

Open main project properties -> Java Build Path -> Projects tab and add there projects the main project depend on.
Switch to Order and Export tab and uncheck Android Dependencies
Enjoy

If you tried all above hints and it still doesn't work try this solution, it worked form me:
Right-click on the Project in the Package-Explorer, click Build Path -> Configure Build Path...
Select tab Order and Export
select library that you can't reach code and then click on button Bottom
Then click on Apply and Close
hope this can help you

Most of the time it happens when specific source folder are not added in build path Sources tab.
Right-click on the Project in the Package-Explorer, click Build Path -> Configure Build Path -> Source Tab
Add the source folder if your project source folder is not there.
Select Add folder -> select your project source folder specifically. Eg: project_name/src . Then Apply it and restart server.

Related

Editing workspace source associated with jar file in same workspace

I have a project A in eclipse whose code is compiled into a jar and used by a separate project B.
I have associated the jar to the workspace project A code.
When I run project B I get stack trace on the console which references the jar.
When clicking on the references the correct file opens up from project A but as read only, I want to edit this file and fix my code.
What I end up doing is open the file manually using search typing the name or navigating to it in the package explorer and then edit it.
I mean file is there in the project in my workspace, why is it not editable?
Is there any way around it?
To open a Java file of project A with the Java Editor (editable) instead of with the Java Class Viewer (read-only) when you are in your project B, you have to do the following:
Right-click project B and choose Properties
In Java Build Path, tab Projects click Add...
Select project A and click OK
In Java Build Path, tab Order and Export move project A to the top by selecting project A and clicking UP several times
Click Apply and Close to close properties dialog of project B
I would recommend convert project A to a plug-in project. A plug-in JAR can be used in both, a plain Java and an OSGi application. The code would be more modular and changes in project A would not require a JAR file to be updated in project B.

Eclipse+Maven src/main/java not visible in src folder in Package Explorer

I'm wondering why I can't see following folders in src node in Package Explorer
src/main/java
src/main/resources
src/test/java
src/test/resources
I can access those folders as direct children of the project and they exist in the file system.
I'm using Spring Tool Suite (STS of Eclipse).
Maybe I can configure Eclipse somehow ?
I have solved this issue by below steps:
Right click the Maven Project -> Build Path -> Configure Build Path
In Order and Export tab, you can see the message like '2 build path entries are missing'
Now select 'JRE System Library' and 'Maven Dependencies' checkbox
Click OK
Now you can see below in all type of Explorers (Package or Project or Navigator)
src/main/java
src/main/resources
src/test/java
Eclipse filters out folders that are marked as source from the "raw" folder path. That is, they are visible only as source folders:
If you can't see them in either place, then they
are either filtered out in the settings: check your settings for Package Explorer (the Package Explorer bar, downwards menu arrow -> Filters...)
or they were created externally and Eclipse haven't noticed them: Refresh your project in this case.
or they don't exist: right-click on the project, select New->Folder and input the path, e.g. src/test/java (not "Source Folder"). After you use Maven->Update Project... on the project, they will be automatically added as source folders, provided you have the default configuration.
Now, as I said, those folder will only be used as source if you preserved the default configuration in your POM. If you defined other resources and/or testResources, those will be used instead. In general, Eclipse m2e synchronizes Eclipse's project source folder configuration with what's in your POM.
EDIT: maybe this is unclear - see those folders at the top? The ones labeled with the /-separated paths? These are your folders. These are the same folders that you would expect to find in main and test, just represented differently.
I used to get the same problem, but solved it by pointing to the right jre used for the project.
Right click on the project
properties
java builpath
see the jre selected
edit it
select alternate jre
installed jre
select the right one
ok
After changing right click on project>maven>update project
Hope it helps.
Attaching screen shot.
If you are looking for Simple/quicker way, you can follow this solution.
Right click on your project.
Goto > Build Path > Configure Build Path > Java Build Path
Goto 'Source' tab, there, you can see like <<your_project_name>>/src/main/java(missing).
Click on it and remove.
Click on Apply and Close.
Now, right click on project and >New > Source folder > add source folder "src/main/java".
happy learning and do not forget to upvote :)
I had the same problem, I changed my Eclipse project view from Package explorer to Project Explorer.
After 2 hours of trying everything...
Just go on window -> show view -> project explorer
and in project explorer you can view of all files solution
I had a similar issue when I checked out a web project from a github repo on my eclipse. src/main/java was directly inside the project root in Package Explorer. My expectation was that src/main/java be visible inside a source folder "Java Resources". There were few things which I did to achieve this.
Right click on Project > Build Path > Configure Build Path..
Select filter "Java Build Path" and click on Tab "Libraries"
Verify your "JRE System Library". If it is not pointing to your latest JDK, then you can click on Edit Button and follow the subsequent dialog boxes to select most appropriate JDK home path in your system.
Once done click Apply, Apply and Close, Finish to close all the associated open boxes for the current filter.
Select filter "Java Compiler" and ensure your JDK Compliance points to correct JDK. Click Aapply
Select filter "Project Facets". Ensure both Java and Dynamic Web Module is selected with correct version.
Click Apply and Close.
Source folder "Java Resources" gets created with src/main/java in it when viewed in Project Explorer.
Navigate > Show In > Package Explore
Right click the Maven Project
-> Build Path -> Configure Build Path
Go to Order and Export tab,
you can see the message like '2 build path entries are missing'
Now select 'JRE System Library' and 'Maven Dependencies' checkbox
Click OK
I have solved this issue by below steps:
Right click the Maven Project -> Build Path -> Configure Build Path
In Order and Export tab, you can see the message like '2 build path entries are missing'
Now select 'JRE System Library' and 'Maven Dependencies' checkbox
Click OK
Now you can see below in all type of Explorers (Package or Project or Navigator)
I used this tutorial to create my maven web project http://crunchify.com/how-to-create-dynamic-web-project-using-maven-in-eclipse/ and eclipse did not create src/main/java folder for me. When i tired to create the source folder src/main/java eclipse did not let me. So i created the folder outside eclipse in the project directly and then src/main/java appeared in eclipse.
I was not able to see the build path option in the properties as well. Also the
src/main/java
was not visible in Project Explorer. below solution worked for me
Go to Project root
Select "Project facets" from Properties
Check "Java"
This fixes the issue
If none of the answers worked for you. You might be in the wrong "Window". I was in "Package explorer" and switching to "Project Explorer" showed me the folders.
Right click on the project > Build Path > Configure Build Path > Source > Add Folder > Select src/main/java > apply and close > refresh the project.
This error happens when there are no files inside /src/main/java
Just make some empty files inside and the problem will go away.
A side note: lots of version control systems (mercurial for example) do not commit folders if there are no files inside.
After creating the project go to properties --> build path --> configure build path --> order and export tab and check jre and maven dependencies. You will then have the folder.
Right click on eclipse project go to build path and then configure build path you will see jre and maven will be unchecked check both of them and your error will be solved
My problem was kind of the same at first and then a little different in the sense that when /java folder showed up, it was deep down in a nested folder somewhere in src/main/resources/java.
Initallially the problem was being in the Package Explorer and not in the Project Explorer as many people have talked about. So,
a. right-click on your project root
b. show in, and select Project Explorer
However, the main problem was I missed to notice a checkbox at the second step of Maven Project Creation from the wizard. That got me created a complicated structure and not a clean direct one.
Once I marked it checked I got a clean project structure as what asked.
I was also facing the issue but I open POM xml file and keep it open. Wait for some time till maven read pom then Automatically it will come.
I tried all the solutions mentioned in this post but none of them worked for me.
The solution for me was to go to build-path and then into Source tab. I found src/main/java folders present there and there was no error. I double clicked on this folder and a window opened like this:
Do not make any change and click on Finish. The folders will appear suddenly.

Eclipse - How to give dependency between projects?

I have two java projects in eclipse. Second one is using first one's jar. When I try to navigate to first one's class from second one, it opens .class of that file. But I want to open the first one project file. Please help.
Thanks in advance.
If you want to link your second project with your first project, don't add the first project in form of a JAR file to the second.
Instead open the project properties of the second project and select "Java Build Path". On the right side on the "Projects" tab you can add your first project as "required project on the build path".
The class files of project 1 are now added to the class path of project 2. And if you click on a class name you directly get to the source code.
Ideally, the JAR of the first project would appear under the "Referenced Libraries" of the second project.
Right-click on that JAR, and choose Properties -> Java Source Attachment.
Provide a variable that links to the source files of this JAR.
Alternatively, install a decompiler plugin (see jd-eclipse) which will decompile class files when you navigate them in Eclipse, so that you can see the source.
Make one project in Eclipse depend on another so that the dependencies are available in the indexer
Tested on Eclipse IDE for C/C++ Developers 2022-09 (4.25.0) on Linux Ubuntu 18.04.
For C++, right-click on the project which depends on another project. Go to "Properties" --> Project References --> check the box next to the project whose files and resources you'd like to be available in the project you are editing --> click "Apply and Close".
The indexed resources in the project next to the box you just checked (glib in the screenshot below) are now available to the project whose properties you just edited! This means if you Ctrl + Click a variable which is defined in the project you are editing, your indexer will now jump to its definition even if it lies in the referenced project (glib in this case)!
Screenshot:
Done!
Old and wrong answer (but may be helpful for adding includes to your project)
For C++, right-click on the project which depends on another project. Go to "Properties" --> C/C++ Include Paths and Symbols --> click "Add Include Path from Workspace..." --> choose the project from your workspace which it depends on, and drill down into the folder of interest --> click "OK". Click "Apply and Close".
You've now made your project whose properties you just edited depend on the other project in such a way that the dependent project dir you just added will now be indexed!

In Eclipse, how to copy an existing project to another project?

In Eclipse, I have one existing project, A. Right now, I have just created another project, B, which is empty. Is it possible to copy all the files of project A, including its source code and related libraries to project B? There are a lot of involved libraries in project A. How to do this copying process correctly to ensure the copied files can still be compiled?
This question appears in Google search as top result for query "copy project in eclipse".
To copy project in Eclipse:
1) right click on project in Package Explorer view;
2) choose Copy;
3) right click on free place in Package Explorer view;
4) choose Paste;
5) enter new name in the prompt window.
To answer actual question, the best way is to delete project B and after -- copy project A as explained above and give it name B.
Close Eclipse
Copy the folder of existing project to anywhere on your disk.
Start Eclipse. Turn off the automatically build.
In the Eclipse do Refactor / Rename to the new project name. (If it connected to CVS/SVN disconnect before rename.)
Import back the old project from place where you copied in the second step.
Turn on the automatically build.
Enjoy them!
You can Import the project
OR
Assuming both project A and B are of same type:
You can copy the contents of src folder as it is.
For the libraries, just go to the build path and add them in the ssame way you did for project A.
Since B is empty, you can just copy project A in the Package Explorer and name the copy B.
Create a duplicate/copy of an existing project (in the workspace).
Then in Eclipse, click file->import
Select import existing projects into workspace
Check the radio button "Select root directory"
Browse your project (the new file you copied in workspace in step 1)
Done!
In the project Explorer, right click your old project, click "Copy", right click again click on "Paste" this time, change the default given name "Copy of My_Old_Project" to any of your choice, after the copy is done, go to the "Search" tab in Eclipse, then to --> "Search", on the File Search, type the old name of your project, make sure that your scope is only "Enclosing project", change all occurrences into the new name, run the project, you are all set...
only thing I noticed about this is that the url will still be of the old project.
For a Gradle project, I had to add two extra steps to mOna's answer, which I will copy for completeness.
Create a duplicate/copy of an existing project (in the workspace).
Then in Eclipse, click file->import
Select import existing projects into workspace
Check the radio button "Select root directory"
Browse your project (the new file you copied in workspace in step 1)
Edit .project file and change the and the to match the new project.
Edit settings.gradle and change rootProject.name to match the new project.
I assume that the renaming in some of the above answers does step 6, but I couldn't get it to work. No matter what I tried, without those extra steps the code looked ok but dependencies were not handled correctly and errors appeared everywhere. I was even able to run gradle on my project from the command line and it "built", but was actually building the old project.
Eclipse has a option in file menu like import existing projec.From that you can import the existing project with all content.I hope this is the solution that you are looking for.

How do I open the GWT samples in eclipse Helios and GWT 2.1.1?

What is the straight-forward way of importing one of the GWT samples in eclipse Helios + GWT 2.1.1. It seems like the content of the zip file doesn't contain any project files for eclipse.
I tried the following:
create a GWT project 'bla'
Delete the src ad war folder and
replace it with the src and war
folder from the sample
refresh the project in eclipse
hit 'run'
[ERROR] Unable to find
'bla/bla.gwt.xml' on
your classpath; could be a typo, or
maybe you forgot to include a
classpath entry for source?
So I go to the run configuration and remove the path for the example projects from the arguments list. I also changed the name of hosted html page to the one in the sample. Hit 'run' and:
[ERROR] Invalid version number "2.0"
passed to external.gwtOnLoad(),
expected "2.1"; your hosted mode
bootstrap file may be out of date;
Now I need an out of date version of GWT to run these samples? What is going on, I just want to look at a sample to learn a new concept.
Reading the readme.txt in the GWT samples directory I came across a solution that works on Eclipse 3.7. Assuming you have ant installed on your machine, execute the following from the sample's directory:
ant eclipse.generate
This will generate both the .classpath and .project files needed to import the sample into Eclipse and run it.
Here the relevant excerpt from README.txt:
-- Option A: Import your project into Eclipse (recommended) --
If you use Eclipse, you can simply import the generated project into
Eclipse. We've tested against Eclipse 3.4 and 3.5. Later versions
will likely also work, earlier versions may not.
If the directory containing this file does not have a .classpath or
.project file, generate them by running 'ant eclipse.generate'
In Eclipse, go to the File menu and choose:
File -> Import... -> Existing Projects into Workspace
Browse to the directory containing this file, select "Mail".
Be sure to uncheck "Copy projects into workspace" if it is checked.
Click Finish. You can now browse the project in Eclipse.
To launch your web app in GWT development mode, go to the Run menu and
choose:
Run -> Open Debug Dialog...
Under Java Application, you should find a launch configuration
named "Mail". Select and click "Debug".
You can now use the built-in debugger to debug your web app in
development mode.
In eclipse 3.7, instead of Run -> Open Debug Dialog..., its Run -> Debug Configurations. I got both the showcase and mail samples running following those commands. Should work for all of the other samples as well.
The examples that come with GWT SDK were built in different ways. Some are Maven builds, others use Ant. Eclipse can handle these common builds directly, but you have to choose the right option.
So, if you want to open the Expenses project in eclipse, you would look in the projects root directory (i.e. ../gwt2.4/samples/Expenses) where you'll find a file called pom.xml. That is a Maven build file. To import it into your Work space select:
File->Import...
Open Maven->Existing Maven Projects, and click on Next > button
Browse your file system til you reach the Expenses folder. Click on it and Select OK.
At this point it will show you the Projects in that directory in the Projects: area of the window...only one in this case: /pom.xml com.google.gwt.sample.expenses:...etc.
click on the checkbox for that project, Next>
Finally, it ask you to map plugins. You'll need to select in the drop down menu under Actions the required plugins (such as m2e).
In other project folders you may find a build.xml file instead. That's an Ant build file. To import that you do something similar, but different (of course!):
File->New->Project...
Open the Java folder and select "Java Project from Existing Ant Buildfile", Next >
Click the Browse button, dig through your directory to the project folder and select the build.xml file, and Open it.
Project name should fill in automatically, just select the declaration to use in the middle window and click Finish.
Since those tutorial only include the src and war directory, the idea remains to:
create a new project following this tutorial
remove any created class part of the example files which are automatically added (see Issue 1547)
Unfortunately the Google Plugin does not allow to created a new Web Application Project without creating template files. The template files are nice for the first try but annoying if you want to start from scratch with a new GWT application.
copy the src and war directory in place within the now empty project
Following your ticket 5847 (No easy and straight-forward way to make examples work in eclipse),
as xo4yhamope comments, you need the right GWT option.
and did you consider the Issue 5038 about the error message:
Unable to find 'xxxx.gwt.xml' on your classpath;
could be a typo, or maybe you forgot to include a classpath entry for source?
This message usually means you attempted to refresh the browser before dev mode has had a chance to finish loading the module.
The uppercase/lowercase confusion happens because the module has been renamed to 'stockwatcher', but dev mode cannot map 'stockwatcher' to "StockWatcher' until it has finished loading the entire module.
So, it's just a matter of waiting a few seconds longer after the Development Mode pane says I should go to http://127.0.0.1:8888/StockWatcher.html?gwt.codesvr=127.0.0.1:9997 ?
That seems a little messed up. It should wait until it's actually ready before telling me it's ok, because users (like me) are going to go there as soon as the UI says it's ready.
The other confusing part is that the server begins serving the host page as soon as that message in the UI comes up. Because this was happening, I assumed that the server was completely up, even though (as it turns out) it wasn't.
Anyway, waiting a few more seconds seems to resolve the "Unable to find 'stockwatcher.gwt.xml' on your classpath" problem. Thanks!
I'm beginning to suspect this is a bug in the samples. I am adding this answer so it can be marked as an answer when I get confirmation about that.
I was trying out some other samples and came across this page. At the top is a little explanation for how to download and import the sample.
Before you begin
The StockWatcher project
This tutorial builds on the GWT
concepts and the StockWatcher
application created in the Build a
Sample GWT Application tutorial.
If you have not completed the Build a
Sample GWT Application tutorial and
are familiar with basic GWT concepts,
you can import the StockWatcher
project as coded to this point.
Download the StockWatcher project.
Unzip the file.
Import the project into Eclipse
From the File menu, select the Import... menu option.
Select the import source General > Existing Projects into
Workspace. Click the Next button.
For the root directory, browse to and select the StockWatcher
directory (from the unzipped file).
Click the Finish button.
If you are using ant, edit the
gwt.sdk property in
StockWatcher/build.xml to point to
where you unzipped GWT.
Now this is what I call straight-forward and easy.
At that point I had already built the StockWatcher in a previous tutorial but I got intrigued by the fact that it was explained exactly as I tried it the first time. So I downloaded the project and it had the correct eclipse project structure. I tried to import it 'et voila' I have the project in eclipse. I runs out of the box (with warnings). That is how expected the samples to be.
This experience leads me into thinking that the sample projects are malformed (as eclipse projects) and need to be updated. Let's hope my issue report leads to results.
-- Option A: Import your project into Eclipse (recommended) --
If you use Eclipse, you can simply import the generated project into Eclipse.
We've tested against Eclipse 3.4 and 3.5. Later versions will likely also
work, earlier versions may not.
If the directory containing this file does not have a .classpath or .project
file, generate them by running 'ant eclipse.generate'
In Eclipse, go to the File menu and choose:
File -> Import... -> Existing Projects into Workspace
Browse to the directory containing this file,
select "Mail".
Be sure to uncheck "Copy projects into workspace" if it is checked.
Click Finish.
You can now browse the project in Eclipse.
Copy from readme.txt supplied by samples. I tried this method, it's OK.
To import GWT showcase(2.5.1) I did following steps:
Create a new Google Web Application Project
New –> Other –> Google –> Web Application Project
Provide the project name “Showcase” and the Package name is com.google.gwt.sample.showcase
Click Finish
Go to eclipse plugin folder and traverse to the gwt
Under that folder go to gwt-2.3.\samples\Showcase
Copy (Ctrl C) all the contents. The contents are
a. src
b. test
c. war
d. build.xml
e. README.txt
In the eclipse, right click on the Showcase project and paste. Overwrite all the files
The files should start copying without any problem
Right click on the Showcase and Run As “Web Application”
You should have your Showcase working like Gem!
For me it worked like a charm ;)
Ref : http://simplestepswebdev.wordpress.com/2011/05/16/import-gwt-samples-into-eclipse/