I have a gradle kotlin project , where I want to load a file from my resources directory.
My kotlin classes are in src/main/kotlin/packagenames and my resources are in src/main/resources
After gradle build including kotlin plugin (kotlin("jvm") version "1.7.20") my classes are copied to build/classes/kotlin/main/packagenames and my resources to build/resources/main
Now I try to read this files from my kotlin code
object {}.javaClass.getResourceAsStream("/resources/filename")
or
object {}.javaClass.classloader.getResourceAsStream("/resources/filename")
but the files are not found.
When I try to see which directory it looks into
println(this.javaClass.getResource("/"))
results in build/classes/kotlin and with javaClass.classLoader it returns null accessing '/'
This means normally I would have to go up two directories and then access resources directory, but this doesn't work as build/classes/kotlin seems to be the root.
Any Idea why this happens.
Remove /resources/ from the input string and instead try
object {}.javaClass.getResourceAsStream("/filename")
This should work as expected.
Related
I'm working on a kotlin project that I want to execute as a jar. This is all done in IntelliJ Idea and I went about making the jar using the artifacts.
The process I followed was (as illustrated by an Idea guide):
Add artifact (as jar) from project structure
Build jar
Run jar
After this, I get a
'no main manifest attribute, in ____.jar'
What I have tried after reading several stack overflow questions:
Checking that the manifest file is in the correct folder and has the correct path in the artifact
Adding code to the build.gradle file for the jar->manifest portion
Trying the method of moving the manifest stuff into a resources folder
Checking that the jar exists
Moving the manifest stuff into a different folder (java,kotlin,out folders)
Making sure that the manifest file is in the correct format
All of the above has not worked.
Something that is confusing to me is that, even when I alter the manifest file to be in "incorrect" format, it still gives the same error. The path stated in the artifact's details is correct yet there is no difference even when I purposely input incorrect items in the manifest file. Not sure if that is the"real" problem but I'm also not sure how to fix that as well
None of the solutions worked for me.
I solved it in this way:
When setting up the artifact, change:
Meta-inf: (...)\src\main\ (you must remove "java")
Also, there was a problem with resources, solved this way:
When setting up the artifact:
Output Layout > Add copy of > Directory content > resources.
That's all!
For anyone that may have encountered this problem in intellij and did not find a solution in any other posts, what helped me was
Navigating to Project Structure
Going to the Artifacts tab
Explicitly adding a new META-INF/ directory in my jar
Adding the created MANIFEST.MF file to the META-INF/ in the jar FROM THE ARTIFACTS TAB
Rearranging the order for the META-INF/ to be at the top
Building and running
The end result looked like this
while creating the exeutable jar file explicitly create a mainfest.txt file
that should be in order of the directory structure and mainfest.txt file should contain only one line (Main-Class:name of the class containg main method)then run the jar tool
I have a resource abc/xyz.json under src/test/resources. I'm using IntelliJ IDE.
While running a Test for my project, I get the following exception.
Exception
com.google.common.io.Resources.getResource("abc/xyz.json")
fails with java.lang.IllegalArgumentException: resource abc/xyz.json not found.
The Test runs fine with gradle clean build and eclipse
Try marking your src/test/resources folder as Test Resource Root. Attach is the image on how to do it.
Let me know if you need any other information.
Make your resource folder a Test Resource folder, if this does not fix the issue then, close your project, delete target folder and open the project. Hope this helps.
Had the same issue not that long ago, since Intellij outputs the class files into out/production/{project_name}, that path will not start in the src directory but will start in the out/production/{project_name} directory.
It will however see files that are in the project root directory, I'd recommend moving your resource directory into the main project directory then accessing the file with "resources\${FILE_NAME}".
Sometimes I have the same problem, though the folder is marked as resource root. So what I do: I just Unmark it and then mark it as resource root again. Sometimes I need to do it several times and it works. But anyway I think it is Intellij IDEA bug.
I have been using IntelliJ Idea for a while now, but I have always had a problem with the following:
Show Intention Actions (or Option + Enter) > Inject Language/Reference > File Reference
Whenever I try to use this, it acts like the root directory is the same directory as the Java class. However, the way I have IntelliJ set up is so the root directory for the program is the project directory.
This means that whenever I try to inject a file reference, I can not get the file completion, and I get an error but the code still compiles.
How can I make the file reference feature use the project root for its base directory?
I have a dir structure for Intellij 12:
...
...test
- java
- com.mycompany.myproject
- package1 (contains code, etc,.)
- resourcePackage (want to contain .json files but can't mark as a resource)
- myOtherJunk.json
- o o o
- resources
- aResource.json
The thing is if I right click on my package name (com.mycompany.myproject) I can only add packages and not directories (like that of an existing resource folder).
However, I don't want to use that existing resource folder for the .json files that I'm going to read into per my test class.
So, I need something to support:
// this already works for the resources directory per the .json file but doesn't for the
// myOtherJunk.json per the resourcePackage.
InputStream is = MyClassTest.class.getResourceAsStream("aResource.json");
This can be solved in several ways. An example of a good approach would be the following folder structure:
src
main
java
resources
test
java
resources
When this is done, you put all you java classes under src/main/java/com.mycompany package and any resources under /src/main/resources/com/mycompany folder.
To link them together, go to the project properties, and find the Path tab. Mark the src/main/java and src/main/resources as source folders. (see the screen-shot attached)
If you link them together, you'll be able to use getResourceAsStream() method.
If you wonder why you should use the following folder structure - this is standard maven way of keeping things clean and tidy.
Directories Creation
Intellij creates directories when you ask her to create package. It is not an error.
If you create package "com", it will create the dir "com", and if you create a source file there, it will think that the file is in the package "com".
If you create package "com.next.pack", it will create three nested dirs "com", then "next", then "pack", and if you create a source file there, it will think that the file is in the package "com.next.pack".
Directories Structures
Only the path under the source root is taken as a package. Any directory(ies) can be set as a source root(s).
Resources roots
Make practically any structure of directories. Somewhere in it there is the root dir of resources. Right-click it and Mark Directory As ... Resources Root.
Notice, the same method can be used for the directories structures for tests, test classes, and test resources. Look here.
Please use #ContextConfiguration annotation to load the resource files. Please see below example.
#ContextConfiguration( { "/app-config.xml", "/test-data-access-config.xml",application-test.yml })
I have multiple non-jar resources to load within an aar file of Axis2.war.
Most of them are successfully loaded using configuration table where absolute path of
each resource(text files) is specified.
But I have an open-source-based jar that loads resource at the root path(??)
of Eclipse project which is at the same level of "src" folder.
When the classes of the project above are jarred into a lib of aar,
the resources has no way of being read by the class that needs it.
I've put them just outside of aar, within the lib of aar or at the same level of the lib.
They just don't work.
Please remember that this is the case where getResourceStream or something like that
can't be used, because the jar has hard coded the path to be located at the root path of
a project of Eclipse.
Then my question is where the Eclipse-project-root-path-equivalent in Axis2.war which is
deployed in JBoss 5.1.
I am not sure if this question is understood, but please give a bit of advice.
//
Referential images that describe the situation above can be found at
https://community.jboss.org/thread/221937
I used System.getProperty("user.dir") to find exactly where the jar refers to the resources and in
my case C:\Users\MYUSERACCOUNT\Development\server\jboss5.1\server\default\deploy\axis2.wββββar\WEB-
INF\services MYUSERACCOUNT is the root of an AAR in Axis2.war service folder. So I placed the
required resource folder within my account folder ( Windows 7 ) and it worked