Classpath jar files in eclipse - java

I am new to eclipse and I have a general question about referencing external Jar files.
I went to the buildpath -> configure build path and added my two jar files that I wanted to reference as parts of an external library but I can seem to make an instance object of the classes in that jar.
And also I tried just copy and pasting the jar files to the extension folder of the standard library too but this also doesn't work.
How can I use the instance objects of the classes included in these jar files?

Even if you build path to external JARs you have to import the required classes because they do not actually exist inside the same package you are working at.

if u want to add external jars into eclipse follow these steps
Rightclick on project - Select Configure Build Path... and the project properties window will appear showing your build path configurations.
Click Add Variable --Click Configure Variables
Click New -Type a name for the new variable.--Browse to the directory that contains the JAR for the path.
Click Ok to define the variable.--Click Ok to close the preferences dialog.
Select the variable from the list.--Click Extend..
Select the JAR(s) that you want to add to the classpath.--Select the JAR(s) that you want to add to the classpath.-Click Ok to close the new classpath variable dialog.

If you already added jar file into classpath and if it doesn't work I would try below on command prompt
java -classpath example.jar;

Related

Eclipse referencing folders inside a Jar file

In my Java project I have a folder like the one below, where I put inside some icons that I'm going to use with JTree library.
When I run the Jar outside Eclipse, I cannot see my icons.
If I open my Jar with archive utility, I cannot see the folder inside it. I also tried to add different folders and they are never referenced inside the final Jar.
So, is there a way to reference the folders in the Jar file?
You need to ensure that Eclipse knows that while building the Jar it has to include the newFolder inside the Jar. You can do that in Eclipse by following the below step in :
click project -> properties -> Build Path -> Source -> Add Folder and select the new folder you just created.
Now create you Jar and you will see your folder in the Jar.
Either move the folder (myFolder) into src directory or mark it as a source folder.

How to include resource folder in executable .jar file in eclipse?

I need to create an application for sorting various types of polygons using various parameters such as height, volume or base area. Arguments for Filename which has parameters for polygons, Sort type, Sort method will be pass through command line.That file is in my resource folder outside my src folder in a project. I have implemented all programs, It works fine when I run using pass arguments through eclipse run configuration. But when I try to run my .jar file using cmd same arguments it gives me FileNotFoundException.
I opened my jar file using 7zip and noticed it never extracted my resource folder in .jar file. I searched online and tried including my resource folder in to build path of eclipse. But still does't work.
Follow these steps:
1) click project -> properties -> Build Path -> Source -> Add Folder and select resources folder.
2) create your JAR!
EDIT: you can make sure your JAR contains folder by inspecting it using 7zip.
Reefer this link as well How do I add a resources folder to my Java project in Eclipse
First, you need to create a source folder for resources, for instance name it res, and then move your image folder to res. When you generate the jar file, you will see the image folder, not the res folder or files separately.
This comes down to how you are generating the JAR file.
When you're exporting the jar in eclipse make sure to checkbox the button that says "Export java sources and resources" https://www.cs.utexas.edu/~scottm/cs307/handouts/Eclipse%20Help/ensureJavaFiles.jpg
There are a lot of ways to do this one is to use Gradle is the recommended way, something like this will work Creating runnable JAR with Gradle

How to import an external jar file and export project as a file system in Eclipse

I added an external jar file into my eclipse project from the properties>libraries in my project. Then I export my project as a file system, to make runnable for every computer. However after I import to project in eclipse, jar file refers to the my computers path thats why it does not work.
How should I solve this ?
Thanks
Define a class path variable and refer to the JAR using a variable in the class path of your project. Each programmer can then set the variable according to theirs environment. Variables can be set in Window -> Preferences -> Java -> Classpath Variables and used by pressing Add Variable... in Java Build Path editor of your project. When adding a variable to classpath, you can even press Extend... and "extend" your variable with a suffix - i.e. have the variable contain a folder name and suffix it with a fixed file name.
Some more obvious options:
Copy the JAR inside your project, then export it.
Agree on a common workspace structure and use a relative path to refer to the JAR.
Use a dependency management tool (e.g. Maven).

Adding .class files to an Eclipse project

This is a total newbie question, but I can't figure it out so I figured that I would ask it here and see what happened.
Here's the problem: For my java programming class, we are supposed to download a .class file created by our instructors containing a custom-made class with methods that we are supposed to use in an assignment. I already know all of the code to create the class within my actual program, but I can't figure out which directory to place the .class file in. I have tried the src and bin folders inside the project directory, and even placed it directly into the project directory, but nothing seems to work. My instructors say to just put it in the same directory as my java program, but they are using jGRASP. What am I doing wrong?
You can place the class file anywhere, just be sure to add its directory to your project's build path. Right click on the project, then properties at the bottom then build path, til you find add class folder.
You have to create a project, or open your project if you have it already, then, while you are in the Java view, select File > New > File. It will open a window, you can create your file there and choose in which project folder do you want to put it. If in that window you press the button "Advanced >>" it will give you the option to "Link to file in the system", check it, and then you can browse your folders to the downloaded file.
I don't know if this solves your problem, it's what I understood from your question.
Your instructors class file must be in the build-time and the run-time class paths for your project to use it. Instructions assume that you are on a PC.
Run-time class path setup
Create a directory (perhaps C:\JavaRoot\classes). I will refer to this as the Classes directory.
If one does not already exist on your PC, create an environment variable named CLASSPATH
Add the Classes directory to your CLASSPATH envoronment variable
Download the class in question and put it in the Classes directory.
Build-time class path setup
Right mouse on your project in eclipse.
Select "Properties".
Select "Java Build Path".
Select the "Libraries" tab.
Click the "Add a Class Folder" button.
Browse to and select the Classes directory you created during "Run-time class path setup" above.
Click the "OK" button
For Eclipse use the following:
Project > Properties > Java Build Path > Libraries (Tab) > Add External Class Folder (Button)
If you have a configured project in Eclipse and you manually added a Java class file in some of the project directories, you just need to refresh the project for Eclipse to recognize it:
Right click in your project (in "Project Explorer" view) -> Refresh

Java - Problem with the classpath on Eclipse

I'm trying to recompile a project I've been working on and I keep getting an error message when trying to load a property file:
The system cannot find the path specified.
I guess this has to do with the classpath. But I've added the path to the file in Properties-> Java build path-> Libraries (external class).
I also checked the .classpath file generated by eclipse, and the path is really there!
Why isn't Eclipse looking at the right path?
There 2 different classpaths, build classpath and runtime classpath. The one you are setting is the build classpath.
Check your runtime classpath by going to Run -> Run Configurations and select your application configuration. Check the classpath setting there.
There is another workaround for this also. Eclipse by default will include your output folder (usually named bin) in your classpath. Typically anything that are not compilable in src folder will be copied to bin as is. I assumed your property file is not located in src folder. What you can do is to open your project property and add the folder where your property is located into Java Buld Path -> Source (tab). This way eclipse will copy the content of that folder into bin and will be in the classpath.
There are several ways to read a property file:
Have it in the current working directory (the one cd'ed to). You can do this in the Eclipse launch configuration. (Run -> Run...)
Include it in your application, by having it in a source folder. You then need to read it in through a class loader to be able to get it always (when jarred up, through Java Web Start, etc).
Double check if the property file or its directory is in the excluded list of the project Source. If it is remove the exclusion filter and try recompiling.

Categories