Path in Maven import being overriden by main project - java

I've got two Maven projects A and B. I'm packaging project A as a jar and adding it as a dependency in project B. Project A shows up in my External Libraries (using IntelliJ) and I can see all the source code and files.
In project A I've got a method that is retrieving the files in a folder located at projectB/src/main/resources/folder/, and I'm using the following code to check whether any files exist within this folder:
File folder = new File("src/main/resources/folder/");
File[] defaultDefinitions = folder.listFiles();
This works correctly when project A is ran. However, when project B calls this method, instead of appending the path to project A's working directory, it is appending it to project B's, and obviously there are no files located at projectA/src/main/resources/folder/.
How does one go about solving this issue?

The problem you're having is that you're using a relative path, and you're relying on working directories (automatically managed by the IDE). You need to read files in a way that's going to be predictable, even if you run your code outside the IDE.
You can solve the problem by using absolute paths, which is going to help even if you run the program from the command line, or anywhere outside the IDE:
String path = "/absolute/path/from/properties/etc/folder/";
//better use a program argument or properties file for this
File folder = new File(path);

Don't load Maven resources from the filesystem API :
File folder = new File("src/main/resources/folder/");
It will work only during the development phase as you have the source code besides the running application.
Instead, use the classloader :
File folder = getClass().getResource("/folder").toURI().toURL().getFile();
It will work as src/main/resources is a "special" directory for Maven as all resource files located in this directory are added in the classpath at compile time and will be also available in the classpath at runtime.
Note that you could retrieve the File from a less convoluted way by using the java.nio API :
Path folderPath = Paths.get(getClass().getResource("/folder").toURI());
Stream<Path> pathStream = Files.list(folderPath);

Related

Files duplicated after manually renaming

Using Intellij, Springboot.
When I rename the files in a directory manually(Intellij->refactor->rename) and restart my web application, when I walk in that directory I can see the renamed files are duplicated (there now is both old name and new name in my app)
What am I missing?!
my directory is under /resources/my_dir
File myDirectory = new ClassPathResource(MY_DIR).getFile();
try (Stream<Path> paths = Files.walk(queryDirectory.toPath())) {
paths.filter(Files::isRegularFile).forEach(path -> setFile(path));
}
I'll assume here, that you are talking about files in the directory of compiled files. The reason is that only the source files are renamed when renaming something like an XML file in the resources folder, or whatever the case may be.
That, however is not in any way tied to the target directory. Build tools, like maven, or I assume the compiler intellij uses copies everything it can't compile to that target directory.
What you have to do is clean the project and build again, so that the target directory is "clean"

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

Eclipse Files Not Found? Works with JCreator

Well I have these .java files in JCreator.
When I compile them in JCreator, it's fine, all files and folders are read and no errors occurred. But, I need to compile them in an Eclipse project to pass it to my professor. Then the problems started.
I added the .java files in a new Eclipse project and into a new package inside the
src folder
Then after that, I made a new Folder in the project called
res
and added it to the build path as a source Folder and added the files and folders in it.
>Use as Source Folder
All sub-folders are fine same name and stuff, Refreshed the project a few times, checked the build path, but when I execute the program, It says:
java.io.FileNotFoundException: sounds\bgmusic.wav (The system cannot find the path specified)
from this line:
InputStream test = new FileInputStream("sounds\bgmusic.wav");
Same folder name, same file, added it to the Build Path and stuff. Also, I repeat, It does not find ALL the files in the resource folder: Pictures, Sounds Fonts and other stuff. I already made some projects in Eclipse and I had successfully added pictures and sounds in them. But what's the problem with this?
Thanks for the help!
Try this:
ClassLoader.getResourceAsStream ("sounds\bgmusic.wav");
I hope it resolves your issue.
You can also refer below link:
http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html

Use .jar with referenced file paths in other project

I have 2 Java projects, one is a Web Project in NetBeans and the other is a Java Project in Eclipse.
Just to know, the Java Project from Eclipse is used by the Web Project in NetBeans as a library (.jar)
Situation:
1. Java Project from Eclipse has the following structure:
And inside the src, there is a class that uses the file1, file2, etc in some method using global variables like this:
public static final String PATH_ONE = "./files/file1.xml";
public static final String PATH_TWO = "./files/file2.xml";
...
Finally, when I test the method in some main() class everything works good.
2. Web Project from NetBeans has a jar reference to the above library and If everything works good, then the web project will be able to execute the method that uses the global variables without problems from the library project.
Problem:
When I run the web project and I want to invoke the method from my library that uses the above xml files from it, for some reason, NetBeans or the project (I don't certainly know) looks for the path and fails in Exception because the path can not be found (It seems that tries to find the path in the web project and not in the library one).
How can I solve this issue? It sounds simple but I don't want to change my structure, load them as a resource or transfer files from one project to another and use external paths to make this work because I just make a recreation of the situation but I am working with lots of files with different folders and paths too.
If you unzip your jar you can't find your folder file because not is included in your classpath.
You can See .classpath file and the folder isn't. For these reason is FileNotFoudnException.
You can to add how source folder in Eclipse ID:
1. Right click in your project/Build Path/ New source folder
Create the source folder . Then you can add a new package with the name Folder.
Your project:
src
resources
folder
resource1.xml
resource2.xml
2. Modify your java project.
public static final String PATH ="/folder/resource1.xml";
Now, if you try to unzip the jar, you can see that the folder was added in the .classpath.
It works for me. I tried to paste image but i'm new user.

Accessing a file within Java project

I have a Java project which uses a third party application. I have the license file (.lic format) stored in the resources folder. Upon running the Ant script, it will copy this file into the /lib/jar directory as it rolls up the project into a Jar file to use on the server. This is where I will need to access the file when running the system live. Here is how the folder structure looks
MyProject
src
package
AccessingClass.java
resources
File.lic
lib
jar
File.lic (upon copy from Ant)
I am not sure the best way to do this so any suggestions other than how I have been trying will probably be helpful. The 3rd party project has a method in a class like License.setLicense(), which can either take a String to the location or an InputStream of the file.
I have been playing around with feeding it an InputStream, but always get a null value when calling getClass().getResourceAsStream(). Here is everything I have tried:
getClass().getResourceAsStream("../../../lib/jar/File.lic");
getClass().getResourceAsStream("/File.lic");
And as a backup I also tried (for local builds I figure I would try the resource folder):
getClass().getResourceAsStream("../../../resources/File.lic");
getClass().getResourceAsStream("/File.lic");
Is there a better method to perform this action? Or would someone be able to tell me why what I am trying is failing? Thanks ahead of time.
Are you running this code standalone or in IDE env looks like classpath issue. If you are running at command prompt you have to set classpath to lib dir if in ide make sure you resources dir is in classpath.
First, you need to ensure that the JAR is added in your class path.
Below should work.
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/resources/File.lic");
Assuming File.lic is placed in root folder of the jar.

Categories