Java: Cannot find package folder path - java

I have a project with the following package set up:
-src
-Interface
-Resources
-images
I want to find the path to the Resources package folder.
By using getClass().getClassLoader().getResource(), I am able to get to the images in the images folder but I also want to get the path for the folder Resources. As I do not know exactly what files will be there it is not possible to look for a determined resource. I am using the JFileChooser to enable the user to select the file, and the aim is to set the default directory to open in the Resources directory.
It works properly when running the project from NetBeans, but when I run the dist executable jar it does not find it as the path returned goes through the jar file. Any idea how to solve this situation?
So... I have been able to find the path returned whenever the project runs in Netbeans and when I run the dist jar. Follows the paths returned:
netbeans: "Scheduler/Resources/"
dist: "Scheduler/Scheduler.jar!/Resources/"

Related

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

What is the proper way to deploy Java project that has JSON files?

I am trying to deploy the program by making it into an executable jar file.
When I run the program directly from IDE (IntelliJ), everything is fine. But after I make jar file out of my project, when I run that jar file, I get errors like:
java.io.FileNotFoundException: src\JsonFiles\Words.json (The system cannot find the path specified)
Don't use relative paths to src folder, open an input stream to the file where it's located relative to a class:
InputStream in = SomeClass.class.getResourceAsStream("Words.json");
where Words.json and SomeClass.java exists in the same folder/package.

Trouble making executable jar

When I try to make an executable jar in netbeans I get this error
C:\lwjgl\lwjgl-2.8.5\res is a directory or can't be read. Not copying the libraries. Not copying the libraries.The res folder holds files like jpgs and wavs that the program relies on to function. I'm using lwjgl, would that be part of the problem? What could be causing this?
try this
Right click on your project folder and click on >>>>clean and
build<<<<
right click on your project folder click on >>>Properties<<<< you
should see the location of your project folder normally this should
be in your documents folder under NetbeansProject folder. locate
your project name folder inside is a folder called >>>dist<<< in
there you should find your project name with a small java cafe
image which is your jar file.

Eclipse , Java Jar Export ,not loading resources

I am building a java desktop application in Eclipse .
And i need to either export it as a JAR and run through command prompt or export as an Executable JAR.
The project structure is
-- src
-- resources
-- bin
-- build
-- Icon
-- build
And after exporting it , when i try to run the jar file , the icons are not getting loaded in the interface .
Please help me out in this regard .
If you put the images in a resource folder instead of a normal folder, they will export.
Make a new resource folder by rightclicking your project, new, Resource Folder. Add the path you want (like 'src/java/resources') and put the images in there.
(at least, this did it for me.)

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