Debug point in Eclipse going in .class file - java

In Eclipse have an Ant project A. For this project in the lib folder by adding a jar, added a dependency of other Java Project B. When I add debug breakpoint in the Java project B, it goes in the .class file instead of .java file.
Also in the .class file during debug if we try to see the details at break point it gives following
Cannot make a static reference to non static field
In the debug view added the source folder of the Project B but still facing same issue. How can this be resolved

As you have added a jar file. That means the project has reference to only the jar file which contains only the pre-compiled class and not the actual source codes.
To have the access to the source codes, add a dependency using the other project as a library project in eclipse.
This way while debugging you will have access to the actual source code.
Make sure you remove the jar file before doing this and also do a clean rebuild to avoid and redundant references.

When eclipse opens the break point on the .class file you need to locate the jar file and then map the sources of the jar. By doing so it does not matter to where the breakpoont is mapped. Eclipse will then open the relevant sources.

Related

JAR files created by IntelliJ IDEA do not run

I wrote a program that spans a few classes in IntelliJ and it works fine when I was testing it in the IDE. However, whenever I follow tutorials to make my project into a .jar executable, it does not run.
The file in the out folder does not run when double-click on it and says "The Java JAR file "projectName.jar" could not be launched.
When opening it from the terminal, I get the error "Could not find or load main class". I used "$ java -jar projectName.jar "
I followed these steps:
Open Project Structure, Artifacts and click the (+).
Under JAR, from modules with dependencies.
Point to the class with my main method and click OK.
Click apply and OK to exit.
Then I close project structure and open Build and then Build Artifacts...
Then I click Build.
Navigate to ~/IdeaProjects/projectName/out/artifacts/projectName_jar/projectName.jar
Then I would try to run it but it does not run. My project has both a GUI and can be run from command lines. When I compiled the main method in Terminal, it worked fine, but I need a JAR file. I'm not sure what I should include in the question, since I'm sure that my code is unrelated. I also utilize Maven, not sure if that is related. Are there other things that I should be doing or adding to my project before I build the JAR?
This can be a result of the Manifest being created in the wrong place.
First make sure it is in src/main/resources/META-INF/MANIFEST.MF
(Intellij sometimes incorrectly places it in /src/main/java/META-INF/MANIFEST.MF)
Then Make sure the jar is looking for META-INF in the src dir NOT in /java OR /resources. This means:
When you set up File>Project Structure>Artifacts>+>JAR>From modules with dependencies... then your Directory for META-INF/MANIFEST.MF should end at src. Example:
ONLY C:\Users\User\StudioProjects\Project\src\
NOT THE PREPOPULATED C:\Users\User\StudioProjects\Project\src\main\java
If your Project Structure jar is already set up change the Manifest File path to:
C:\Users\User\StudioProjects\Project\src\META-INF\MANIFEST.MF
INSTEAD OF C:\Users\User\StudioProjects\Project\src\main\java\META-INF\MANIFEST.MF
You need to make sure that the Main-Class attribute is set. One way to check this is by opening the Jar with a Zip utility and looking for META-MF/MANIFEST.MF extract or open that file with a notepad, notepad++ or any document reader. If "Main-Class" is not in their or set to the correct main class you will received the error you have received. Seeing the IntelliJ tutorial it has a Main-Class field when setting up artifact publishing. I have included the link below to the IntelliJ page.
IntelliJ Creating an Artifact Tutorial

No source attachment in Java

I am trying to use an external .jar file in my project. I have put that .jar file in my project's /lib folder, and I have referenced it in the build path. However, when I try to use it in code, I can't get the javadoc for it, nor I can see the actual code from the referenced classes. It says it has no source attachment. What is it meant by source attachment? I do have a javadoc folder that came with it, but when I attach it to my build path, it still reports it could not found the javadoc for the classes. Any ideas on that?
I want to be able to see the javadoc.
The .jar file is the generated packed classes of the java code. Some jars will come with java doc and code and others won't.
For recent Eclipse versions:
right-click the jar in question (in the referenced jars, not the physical jar) and choose Preferences -> Javadoc. Here give a correct location (zip/URL) to the correct javadoc. (Select Validate!)
For older Eclipse versions:
First: Navigate to the jar itself in the Eclipse project explorer (to the left) and try to open aclass of the jar. Then, you will see the warning that it has no source attachment, and a button to attach the source code.
Then, push the button, select "external folder" and then navigate to the folder with source you already have.
hope I got you right:
try to right-click the jar in the references jar tab and go to Preferences -> Javadoc.
choose the right location to the correct javadoc and press "Validate" afterwards
Jar files can be built with or without source code. It's normal for 3rd party applications to produce a binary jar and a source jar, but it isn't required. The message is telling you that it doesn't know where the source code is for the library you are using. It's been a long time since I've done this in eclipse, but there is a way in the project settings to point each library jar to the corresponding source location (which can be a jar, zip file or directory).
Here are a couple of references
Attach the Source in Eclipse of a jar
Attaching the source to a library in eclipse

Eclipse Debugger throwing Source Not Found Error

I'm using Eclipse to debug a Java program I'm developing. Upon stepping into an insert method of one of my classes called from another class I get a source not found error. I have no idea why because when I hit F3 to go to the class' definition it does so without a problem. I'm using Eclipse Kepler
The class in question is found in a jar file on your classpath and jar files do not correspond to editable source. (but it may have a read-only source zip file attached).
This is most likely because the jar file is placed before the project containing the class on the launchers build path.
Fix your build path.

Error during creating jar file in Eclipse

I d' like to create a jar file. In Eclipse I go to file -> Export. But, unfortunately I get an error:
... is not on its project's build path. Unable to get package fragment root...
I think you might miss a JAR that you're using through imports, you've to fix your Java build path.
To do that, right click on the project, choose Properties, select Java Build Path, click on Libraries tab, Add JARs, add the missing JAR.
If it doesn't work try to restart Eclipse as well and give us a little more context.
I experienced same error while exporting prepared class files through .jardesc (stored export description). Export was adding a lot of prepared class files (so no source .java files) to jar. Due to some changes to java build path source files have been added next to prepared class files. This caused that export did not use given class files. Removing source files fixed error. I had to fix java build path then to prevent source files appearing next to prepared class files again.

In a big Java project can not locate .java file belonging to a package using eclipse

I'm new to java with a C++ background. I'm using Eclipse and have imported projects created by maven using "mvn eclipse:eclipse". In eclipse, when I highlight a class name from another class and do "Open Declaration", eclipse would normally open the corresponding .java file.
However, for one of the classes, eclipse "Open Declaration" opens up a .class file which shows all the context of the file. However, this file is not editable. Furthermore, I can't locate the actual .java file behind it. There is a package line in the beginning of the .class file and it points to a directory that doesn't seem exist. I believe there is a way to trace back to the original .java file from the .class file but I'm not sure how to do this. Please help.
The class is likely in one of your dependency jars, so you don't have the source locally in a project. Many maven published projects allow for the source to be downloaded in a jar, and eclipse can use that to display the source, but editing it would be meaningless in that instance.
If you hit ctrl+shift+T and search for the class you should be able to see the location of the class in the bottom portion of the "open type" dialog.
In the eclipse "properties" for the jar that contains the class, you should be able to set the "Java Source Attachment" to the folder on your system that contains the source (if you have it)

Categories

Resources