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.
Related
i know that this question was asked several times before but none of the solutions quite work for my problem (or i just dont know how to adjust them properly).
I try to program an application, that is supposed to open an image, which is located inside of the jar file of the application.
The jar file is created by maven, so originally the picture was in the src/main/resources directory of my maven project and it will finally be in the base directory of my jar file.
The program itself consist of two java files and an fxml file. The one java file is my main class (called imageviewer.java) and the other java file is my javafx controller (called Contoller.java).
The method, that is supposed to open the picture is in the Controller.java file.
The solutions I found were all using getclass().getResources... , but in this case it does not work (maybe because it is not a jar file that will consist of a single class). The name of the final maven-generated jar file will be imageviewer-0.0.1-SNAPSHOT.jar.
How can I access the image inside it?
Okay I did actually find the solution.
In Eclipse I was only able to acess the image while it was in the same folder as the java documents. After I moved it into the "Resources" folder (which is where it belongs in a maven project) i was not able to acess it with a relative path.
All I had to do was creating a new package inside of the resources folder that is called exactly the same as the package in which the java files are.
So my project structure now looks like this:
src/main/java/myPackageName/MyJavaFiles.java
and
src/main/resources/myPackageName/Imagename.jpg
After that i was able to acess is with getClass().getResourcesAsStream("Imagename.jpg");
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
I'm working on a framework-type project in Eclipse, that has a res folder attached with, as you guessed Resources inside of it. At the moment, they are crucial text-files that are to be read.
I also have a main test rig that is apart of it's own Eclipse project, as I plan to expand it in the future. This test rig has included the framework project on it's Build Path, so I can access classes from the framework as they are added.
But this is where my problems lie: If I want to import a class, that works fine. If I want to access a file through an InputStream, or Reader, it breaks. I know this is because the files included from the secondary project aren't placed inside the test rigs bin folder, rather they reside in their own bin in the other project.
Is there a way I can get my main project to access resources from the other without having to do ../../ trickery in my file paths? I plan on making this a real thing and don't want file paths doing things like that.
In eclipse do this,
Build Path ---> Configure Path ----> Click Project Tab ---> Add the project.
Also, you can package the other project in a jar and import it as an external jar.
I have a maven web project in eclipse. I need to get the project's path, actually have to get list of files under src\main\resources\someFolder in project.
Tried String dataDir = "src\\main\\resources\\someFolder";, on running this directory structure is created inside eclipse folder like F:\softwares\eclipse\eclipse\src\main\resources\someFolder. Same when using / instead of \\.
Tried System.getProperty("user.dir")and new File(".").getAbsolutePath(), they return F:\softwares\eclipse\eclipse.
I need to access the project folder in my workspace F:\workspace\Project\src\main\resources\someFolder
But when created a core java app and used System.getProperty("user.dir")and new File(".").getAbsolutePath(), I am getting project path in workspace, F:\workspace\Project. This src\\main\\resources\\someFolder also works fine then.
Why this odd behavior from eclipse?
As mentioned here the directory user.dir is the place where the JVM is started. As web applications are mostly jar/war/ear packages placed somewhere within the folder of the server eclipse handles them in a different way because the behaviour of such a web application is different. You cannot expect to have file access from outside the jar/war/ear file. Within the jar/war/ear file everything from within src/main/resources will be available just by using getResourceAsStream as described in many other stackoverflow articles. This way you mustn't use src/main/resources/myfile.txt but myfile.txt.
Don't try to guess or use what the user.dir / JVM/server start folder is!
I have downloaded some source code that contains src folders with some .java files. I want to try to run the code in netbeans.
This is the structure of the folder :
C4.5/src/main/java/myc45/
and in these folder include some .java files.
What should I do first?
When you create a project in Netbeans, one of the options in the project creation window is create project from existing source. If you have an existing project, you can also edit the project properties and tell it what the source folders are.
As an alternative to #PaulJAbernathy 's solution:
create a new project in Netbeans
via the projects windows, create a package myc45 (the package name used in the code you want to import) - you can do so by rightclicking, new Package
now, inside the src directory of your project directory you'll find a directory called myc45. Drop the source files into that directory using whatever file browser you commonly use. You'll see that Netbeans picks up the files almost immediately in the project explorer.
you can now use the code.
A bit messy, but there are advantages: if eg you want to transform a bunch of existing code files into a Maven type project, this is probably the easiest way.