Error during creating jar file in Eclipse - java

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.

Related

Eclipse IDE - unable to export to .jar: "not on its project build path"

I have been breaking my head for a couple of weeks now over an error I get when I try to export a .java file to a .jar in the latest version of Eclipse IDE (screenshot of export settings / project tree / error)
I am coding for a trading platform, called 'MotiveWave', and I can export other Studies / .java files just fine (so my build path should be ok, right ?)
I get:
JAR creation failed. See details for additional information.
MW/Studies/MotiveWave Studies/src [in motivewave] is not on its project's build path
Unable to get package fragment root: motivewave/MW/Studies/MotiveWave Studies/src/ChoppinessIndex.java
MW/Studies/MotiveWave Studies/src [in motivewave] is not on its project's build path
MW/Studies/MotiveWave Studies/src [in motivewave] is not on its project's build path
Unable to get package fragment root: motivewave/MW/Studies/MotiveWave Studies/src/ChoppinessIndex.java
MW/Studies/MotiveWave Studies/src [in motivewave] is not on its project's build path
when I try to export some of the .java files. All the others export perfectly, without a glitch
Please tell me how I can debug this further, because I am too unexperienced to grasp completely what this particular error message means (and why it only occurs on some of the exports).
For what I can see, you just need to add that source(path to the .java files) to the project build path configuration in eclipse, I would be more specific, but I haven't used eclipse in a while, I found this: https://www.tutorialspoint.com/eclipse/eclipse_java_build_path.htm and I'm quite sure it will help you.
PS: Not a big fan of folders with spaces in their names.

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.

Attach the Source in Eclipse of a jar

I added a Student.jar into my Build Path in my eclipse like this-
Right click on the project->BuildPath->Configure Build
Path->Libraries->Add External Jars
There is one class named StudentTest in Student.jar file. When I was debugging my code in eclipse, I stepped into that StudentTest class in the Student.jar.
And after that eclipse shows me like this-
The JAR file S:\some_location\Student.jar has no source attachment.
You can attach the source by clicking Attach Source below
Now I am not sure how should I attach the source in my eclipse. And from where? Can anyone provide me step by step what I need to do.
Update:-
I tried unzipping the Student.jar and I got Student folder. And after that I tried pointing the source to Student folder But still I am not able to see the class properly so that I can debug it properly, it shows the same above behavior.
Use Java Source Attacher !
It does what eclipse should do - a right click context menu that says "Attach Java Source.
It automatically downloads the source for you and attaches it. I've only hit a couple libraries it doesn't know about and when that happens it lets you contribute the url back to the community so no one else will have a problem with that library.
Eclipse is showing no source found because there is no source available . Your jar only has the compiled classes.
You need to import the project from jar and add the Project as dependency .
Other option is to go to the
Go to Properties (for the Project) -> Java Build Path -> Libraries , select your jar file and click on the source , there will be option to attach the source and Javadocs.
A .jar file usually only contains the .class files, not the .java files they were compiled from. That's why eclipse is telling you it doesn't know the source code of that class.
"Attaching" the source to a JAR means telling eclipse where the source code can be found. Of course, if you don't know yourself, that feature is of little help. Of course, you could try googling for the source code (or check wherever you got the JAR file from).
That said, you don't necessarily need the source to debug.
This worked for me for Eclipse-Luna:
Right Click on the *.jar in the Referenced Libraries folder under your project, then click on Properties
Use the Java Source Attachment page to point to the Workspace location or the External location to the source code of that jar.
Go back in to where you added the jar. I believe its the libraries tab, I don't have Eclipse open but that sounds right. to the left of the jar file you added there should be an arrow pointing right, click that and 3 or 4 options expand, one of them being the source file of the library. Click on that and click edit(I think you can also double click it) then locate the file or folder on your hard disk, you probably have to click apply or okay and you're good to go, same with javadoc and i think the last one is native libraries. I don't pay much attention when I'm in there anymore if you couldn't tell. That's what you were asking, right?
I Know it is pretty late but it will be helpful for the other user, as we can do Job using three ways... as below 1)1. Atttach your source code using i.e, Right click on the project then properties --> Java build path--> attach your source in the source tab or you can remove jar file and attach the source in the libraries tab
2. Using eclipse source Analyzer In the eclipse market you can download the plugin java source analyzer which is used to attach the open source jar file's source code. we can achieve it after installing the plugin, by right click on the open source jar and select the attach source option.
3. Using Jadclipse in eclipse you can do it last not the least, you can achieve the decompile your code using this plugin. it is similar way you can download the plugin from the eclipse market place and install in your eclipse.
in jadclipse view you can see your .class file to decomplile source format note here you cannot see the comment and hidden things I think in your scenario you can use the option one and option three, I prefer option three only if i want to the source code not for the debug the code. else i ll code the option 1, as i have the source already available with.
Simply import the package of the required source class in your code from jar.
You can find jar's sub packages in
Eclipse -- YourProject --> Referenced libraries --> yourJars --> Packages --> Clases
Like-- I was troubling with the mysql connector jar issue
"the source attachment does not contain the source"
by giving the path of source folder it display this statement
The source attachment does not contain the source for the file StatementImpl.class
Then I just import the package of mysql connector jar which contain the required class:
import com.mysql.jdbc.*;
Then program is working fine.
I faced the same issue and solved using the below steps. Go to Windows->preferences->Editors->File Associations
Here click on Add
then type .class
click on OK
again click on Add
then type .classwithoughtsource
click on OK
Now you will be able to see JadClipse option under Java section in Windows->Preferences
Please provide the path of jad.exe file as shown below.
Path for Decompiler-C:\Users\ahr\Documents\eclipse-jee-galileo-SR2-win32\jad.exe
Directory for temporary Files-C:\Users\ahr.net.sf.jadclipse
click on Apply
Now you should be able to see the classfiles in proper format.
It is quite possible, just go to the jar in the Build Path and choose to attach a source just like follow.
Download JDEclipse from http://java-decompiler.github.io/
Follow the installation steps
If you still didn't find the source, right click on the jar file and select "Attach Library Source" option from the project folder, as you can see below.
I am using project is not Spring or spring boot based application.
I have multiple subprojects and they are nested one within another.
The answers shown here supports on first level of subproject.
If I added another sub project for source code attachement, it is not allowing me saying folder already exists error.
Looks like eclipse is out dated IDE. I am using the latest version of Eclipse version 2015-2019.
It is killing all my time.
My intension is run the application in debug mode navigate through the sub projects which are added as external dependencies (non modifiable).
I have faced same problem and resolved it by using following scenario.
First we have to determine which jar file's source code we want along with version number. For Example "Spring Core" » "4.0.6.RELEASE"
open https://mvnrepository.com/ and search file with name "Spring Core" » "4.0.6.RELEASE".
Now Maven repository will show the the details of that jar file.
In that details there is one option "View All" just click on that.
Then we will navigate to URL "https://repo1.maven.org/maven2/org/springframework/spring-core/4.0.6.RELEASE/".
there so many options so select and download "spring-core-4.0.6.RELEASE-sources.jar" in our our system and attach same jar file as a source attachment in eclipse.
Try removing the breakpoints. :)

Add a properties file to IntelliJ's classpath

I'm running a simple Java program from the IntelliJ IDE using the Run->Run menu. It works fine. Now I want to add log4j logging.
I added a resources folder under my project root.
I added a log4j.properties file in that folder.
I changed the code to log something.
What is the right way to tell IntelliJ to include the resources folder in the classpath so the properties file is seen?
With IntelliJ 8 I could guess like a drunk monkey and eventually get it to work. I have 9 now and I am wholly unsuccessful. I've been trying for an hour. How about an "Add to classpath" option somewhere? /fume /vent /rant
Try this:
Go to Project Structure.
Select your module.
Find the folder in the tree on the right and select it.
Click the Sources button above that tree (with the blue folder) to make that folder a sources folder.
Actually, you have at least 2 ways to do it, the first way is described by ColinD, you just configure the "resources" folder as Sources folder in IDEA. If the Resource Patterns contains the extension of your resource, then it will be copied to the output directory when you Make the project and output directory is automatically a classpath of your application.
Another common way is to add the "resources" folder to the classpath directly. Go to Project Structure | Modules | Your Module | Dependencies, click Add, Single-Entry Module Library, specify the path to the "resources" folder.
Yet another solution would be to put the log4j.properties file directly under the Source root of your project (in the default package directory). It's the same as the first way except you don't need to add another Source root in the Module Paths settings, the file will be copied to the output directory on Make.
If you want to test with different log4j configurations, it may be easier to specify a custom configuration file directly in the Run/Debug configuration, VM parameters filed like:
-Dlog4j.configuration=file:/c:/log4j.properties.
I have the same problem and it annoys me tremendously!!
I have always thought I was surposed to do as answer 2. That used to work in Intellij 9 (now using 10).
However I figured out that by adding these line to my maven pom file helps:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
...
</build>
I spent quite a lot of time figuring out how to do this in Intellij 13x. I apparently never added the properties files to the artifacts that required them, which is a separate step in Intellij. The setup below also works when you have a properties file that is shared by multiple modules.
Go to your project setup (CTRL + ALT + SHIFT + S)
In the list, select the module that you want to add one or more properties files to.
On the right, select the Dependencies tab.
Click the green plus and select "Jars or directories".
Now select the folder that contains the property file(s). (I haven't tried including an individual file)
Intellij will now ask you what the "category" of the selected file is. Choose "classes" (even though they are not).
Now you must add the properties files to the artifact. Intellij will give you the shortcut shown below. It will show errors in the red part at the bottom and a 'red lightbulb' that when clicked shows you an option to add the files to the artifact. You can also go to the 'artifacts' section and add the files to the artifacts manually.
Faced a similar challenge adding files with .ini extensions to the classpath. Found this answer, which is to add it to Preferences -> Compiler -> Resource Patterns -> [...] ;*.ini
If you ever end up with the same problem with Scala and SBT:
Go to Project Structure. The shortcut is (CTRL + ALT + SHIFT + S)
On the far left list, choose Project Settings > Modules
On the module list right of that, select the module of your project name (without the build) and choose the sources tab
In middle, expand the folder that the root of your project for me that's /home/<username>/IdeaProjects/<projectName>
Look at the Content Root section on the right side, the red paths are directories that you haven't made. You'll want to put the properties file in a Resources directory. So I created src/main/resources and put log4j.properties in it. I believe you can also modify the Content Root to put it wherever you want (I didn't do this).
I ran my code with a SBT configuration and it found my log4j.properties file.
For those of you who migrate from Eclipse to IntelliJ or the other way around here is a tip when working with property files or other resource files.
Its maddening (cost my a whole evening to find out) but both IDE's work quite different when it comes to looking for resource/propertty files when you want to run locally from your IDE or during debugging. (Packaging to a .jar is also quite different, but thats documented better.)
Suppose you have a relative path referral like this in your code:
new FileInputStream("xxxx.properties");
(which is convenient if you work with env specific .properties files which you don't want to package along with your JAR)
INTELLIJ
(I use 13.1 , but could be valid for more versions)
The file xxxx.properties needs to be at the PARENT dir of the project ROOT in order to be picked up at runtime like this in IntelliJ. (The project ROOT is where the /src folder resides in)
ECLIPSE
Eclipse is just happy when the xxxx.properties file is at the project ROOT itself.
So IntelliJ expects .properties file to be 1 level higher then Eclipse when it is referenced like this !!
This also affects the way you have to execute your code when you have this same line of code ( new FileInputStream("xxxx.properties"); ) in your exported .jar.
When you want to be agile and don't want to package the .properties file with your jar you'll have to execute the jar like below in order to reference the .properties file correctly from the command line:
INTELLIJ EXPORTED JAR
java -cp "/path/to_properties_file/:/path/to_jar/some.jar" com.bla.blabla.ClassContainingMainMethod
ECLIPSE EXPORTED JAR
java -jar some.jar
where the Eclipse exported executable jar will just expect the referenced .properties file to be on the same location as where the .jar file is
Right-click on your directory and from Mark directory as select Resources root as below:
Perhaps this is a bit off-topic, seeing as the question has already been answered, but I have experienced a similar problem. In my case only some of the unit test resources were copied to the output folder upon compilation. My persistence.xml in the META-INF folder got copied but nothing else.
In the end I "solved" the problem by renaming the problematic files, rebuiling the project and then changing the file names back to the original ones. Do not ask me why this worked but it did. My best guess is that, somehow, my IntelliJ project had gotten a bit out of sync with the file system and the renaming operation triggered some kind of internal "resource rescan".
This is one of the dumb mistakes I've done. I spent a lot of time trying to debug this problem and tried all the responses posted above, but in the end, it was one of my many dumb mistakes.
I was using org.apache.logging.log4j.Logger (:fml:) whereas I should have used org.apache.log4j.Logger. Using this correct logger saved my evening.
I had a similar problem with a log4j.xml file for a unit test, did all of the above. But figured out it was because I was only re-running a failed test....if I re-run the entire test class the correct file is picked up. This is under Intelli-j 9.0.4

How do I include .class files in my project in Eclipse? (Java)

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
Right click into your project and select Java Build Path to add new dependencies.
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
or put everything into a jar file and add this as an external jar.
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.
My original answer is left below for context.
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
zip the class folder.
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file

Categories

Resources