I'm using IntelliJ IDEA 2020.1 CE. I have a non-Maven Java project that builds and works fine. I use the built-in build system. The resources are all correctly copied from the source resources folder to the project's output directory; however, since I use a package, e.g., org.acme, I'd like the resources to actually be copied to the output/org/acme folder, so that they end up with the .class files.
Is this possible? Do I need to perform a post-build step? Other?
Thanks,
John
The good people at JetBrains helped me out on this one, so I thought I'd share it here.
To copy Java project resources to a directory relative to the project output directory (where the .class files go by default), use the Relative Output Path for the directory marked as your resources directory.
https://www.jetbrains.com/help/idea/content-roots.html
For example, let's suppose you have a Java project and you put your classes into a package called org.acme. Let's also assume your resources are in a resources/ directory and that your project's output directory is called target/.
What will happen by default is that your .class files will end up in target/org/acme/, but your resources will end up in target/. If you want to copy your resources to the same place as your .class files (it makes loading them easier at times), you can set your Relative Output Path to org/acme/
In the IntelliJ IDE, from the main menu:
File -> Project Structure
Select Modules from the left-hand pane
Select Sources from the main panel
Select your resources/ directory
Click on the pencil icon next to the resources directory
Add your Relative Output Path
Next time the project builds, the resources will be copied to this relative path under the primary output directory.
I hope this was useful!
John
Related
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 want to access the resource's form of my project "\src\main\resources" but for any reason I can only access the target classes.
Here is my code:
System.out.println(Main.class.getResourceAsStream("/123.txt")); // java.io.BufferedInputStream#66cd51c3
System.out.println(Main.class.getResource("/123.txt")); // file:/C:/Users/Raul/workspace/Serial/target/classes/123.txt
System.out.println(Thread.currentThread().getContextClassLoader().getResource("123.txt").getPath()); // /C:/Users/Raul/workspace/Serial/target/classes/123.txt
and here my Project Dirs:
The thing is, even if I delete all the files in the target/classes and run the code, the compiler will copy the files from "src/main/ressources" into "target/classes" and read them from there.
I want to access the resource's form of my project "\src\main\resources" but for any reason i can only access the target classes.
I think the question is answered by user #VGR. Just to clarify it in another words:
You put your resources in the /src/main/resources folder, and these resouces will be copied as is into the /target/classes folder when you build your project.
Example
src/main/resouces/123.txt -> target/classes/123.txt
src/main/resources/myresources/145.txt -> target/classes/myresources/145.txt
...
Now if you run the program inside of your IDE you'll observe the following:
System.out.println(Main.class.getResource("/123.txt"));
output: file:/C:/Users/Raul/workspace/Serial/target/classes/123.txt
System.out.println(Main.class.getResource("/myresources/145.txt"));
output: file:/C:/Users/Raul/workspace/Serial/target/classes/myresources/145.txt
But if you open the generated jar file you'll not see the target folder because the file 123.txt will be on the root of the jar file and the file 145.txt will be under the folder myresources/145.txt.
The folder target is just an output directory for the build tool and will not be packaged within your jar file.
Now to the following question:
the problem is that i dont know how to export the target classes to my jar, or how can I get "src/main/ressources" as return value.
To answer this question you have to look into your pom.xml file on the root of your project. There should be a <packaging>jar</packaging> entry in it. If that is so you create the jar file as follows:
Option 1: from the command line
mvn clean install
the jar file will be created and copied into the folder target.
Option 2: from within Eclipse (for example)
right click on the pom.xml > Run AS > Maven install
the jar file should also be generated and copied into the folder target.
Note: on your screenshot there are two jar files: core-0.0.1-SNAPSHOT.jar and Serial-0.0.1-SNAPSHOT.jar; remove them (mvn clean or right click > Run AS > Maven clean) before generating the jar file. The reason is Maven can only generate one jar file per Maven module / project, afaik.
You are seeing the intended behavior. A Java program is compiled into an executable form—meaning, .class files and resources. When other users run your program, they will not have access to the source, so your code should not assume your source tree will be available.
Simply put, your code is correct as is. Do not attempt to read the source tree. If you want target/classes to contain up-to-date files, rebuild your project.
A word of caution: Never use the getPath() method of URL to convert a URL to a file name. There are many characters which are not permitted in URLs, and those characters will be “percent-escaped” in order to conform to the URL specification; as a result, the path portion of a URL is not a valid filename! The only correct way to convert a URL to a file is Paths.get(url.toURI()). However, you should not even try to convert a resource to a file at all, because once you move on to packaging your programs in .jar files, your resources will not exist as regular files at all, only as entries in .jar files (which are actually just zip files with some Java-specific entries added).
I have xml files in eclipse project's source directory, like:
src/java/main/com/xx/zz.xml
1.When using eclise to build automatically, xml files are copied to target/classes.
2.When using 'mvn complie', xml files are not copied to target/classes.
For the second case, I found this:why xml files in eclipse project's source directory is not copied to target/classes directory?.
but for the first case, I cannot find any document.
Can someone explain it for me ?
Thanks!
Eclipse works quite a bit differently than standalone Maven. Maven uses javac from JDK. By default javac only processes .java files and generates .class files in the same directory as .java sources. Maven asks it to generate classes in a separate directory and javac only moves .class files there.
The reason for this is that javac gives you more freedom in organizing your source files than most developers use. For instance javac does not care if your class is located in a folder structure that mimics declared packages. You can put a module together by putting several .java files along with some other files like .properties or .xml in the same folder. Your .java files can have different package declarations. For instance you can have files A.java:
package aaa.bbb;
class A {}
and B.java:
package zzz.uuu;
class B {}
If you ask javac to put classes in a target directory, it will create necessary subfolders for .class files, that will resemble the package structure. However it cannot do so for properties and xml files, because they do not contain package declarations. Such 'resource' management is left for the build tool.
Maven expects that you put such resources in another source folder (resources). They will be copied to generated package structure, so you should match it in resource folder. This is why it does not copy non-java files in source folders.
Eclipse expects you to put even .java files in a target package structure and complains if your package declaration does not reflect relative path of the file. This is where you have less freedom compared to bare javac. Thanks to this rule it can copy resources along with .class files and does not need another 'resource' folder.
You can configure Eclipse to use source folder as output folder. Eclipse will not touch resources in this case.
If you right click on the project in eclipse and select 'properties', then Java Build Path you see an input at the bottom for the Default Build Path, which should be target/classes. The source folders are shown in the main dialogue. If you click on the source folders then you can modify each, to exclude the xml files (if that is what you want to do).
Maven will include your xml files automatically if you put them in src/main/resources.
If you don't want to have xml files in build directory, you need to configure eclipse excluded source file types -
right-click on the file in the Project Explorer, choose Resource Configurations > Exclude from Build and choose the configurations that you want.
I have Java code managed by a Git repository. Currently, the directory structure is
myProject |
-- src
-- bin
-- lib
-- .git
Currently, each developer creates its own development environment under his desired tool (Eclipse/Netbeans). I would like to create a Eclipse project for this code and add it to my source repository, so that new Eclipse developers don't need to create a new project and set the dev environment manually. I'd like to have this:
myProject
|
-- src
-- bin
-- lib
-- .git
-- eclipse
|
-- .settings/
-- .project
-- .classpath
From what I've read in the web, I don't need to add the .metadata directory to the repository. Also, I know I could have dependency problems in some situations if I add the .classpath folder to the repository, but I prefer to not support these problematic situations (eg different Java compilers) than forcing the dev to manually set the classpath. Finally, I don't want to use Maven in this project.
Concisely, my requirements are
- I don't want to put Eclipse project files in the root folder
- I don't want to duplicate the code inside eclipse folder (that's what happens when I try to import existing code into an isolated project folder). I want it to reference the source files I have in the folder named 'src'
- I want to configure Eclipse to put the compilation output (.class files) in the 'bin' folder.
That latter item I was able to set when creating the project, but I wasn't able to configure Eclipse to reference the source code (not duplicate it) without choosing a source parent directory as the project folder.
Any help is much appreciated.
You will need to create a project in the place you want it, and then use 'linked source' directories to point to your actual source code.
more information:
Store eclipse .project files outside the project directory
Create your directory structure above with the eclipse subdirectory.
Create a new Dynamic Web Application (or whatever, assuming based on .settings)
Uncheck Use default location and specify your eclipse directory, hit Next
Remove the lone source folder
Leave Generate web.xml... unchecked
File > New > Folder and click Advanced>>
Use Link to alternate location (Linked Folder) to create src and bin directories that are linked to the top-level src and bin.
Right-click on the project and use File > New > Source folder to add the linked src folder with an output directory of bin
Move WebContent to WebContent.bak and create a new WebContent directory that is linked to the top level.
Move the contents of WebContent.bak to WebContent
Manually edit eclipse/.settings/org.eclipse.wst.common.component to change WebContent.bak to WebContent (is there a setting for this? couldn't find it)
Refresh the project
Right-click the project and select Java EE Tools > Generate Deployment Descriptor Stub
I have some java source code. It an just an archive which is four folders.
Folders MAIN, DATA, TAGS, USER_INTERFACE. Each folder contains a few class files.
I see how everything works together, but can't see to get the program to import correctly or run. I did find a MANIFEST.MF.
What is the proper way to import such a project?
Create an Eclipse project. Under the project settings, select the "Java Build Path", and add each source folder (and remove the default src folder that Eclipse may add for you).
If necessary, you may set the output/build folders for each source directory, and clear Eclipse's default bin output directory. Check the "Allow output folders for source folders" to enable this, then set each source folder's output folder.
(You may or may not want to keep compiled classes in individual directories.)
If there are class files in the project that aren't generated from source in the project, in the same dialog, select the "Libraries" tab, and click the "Add Class Folder" button to add dependencies from within the project.
It doesn't seem that your source code is an eclipse-importable project (it would have to have a .classpath and .project file in the top level directory for that).
You should instead create a new Java project. For simplicity, set the project's location to be the parent directory of your MAIN, DATA, etc. directories. Then after the project is created, configure a source folder for each of MAIN, DATA, etc. (right-click the project, choose Properties > Java Build Path, then work in the Source tab). Your source folders should then show up in the navigator and the project should be able to be compiled.
Create a hello world project in eclipse and understand the project structure in eclipse.
And then manually import the files it is simple and avoids a lot of confusion especially for somebody new to the environment.