My Java Springboot cloud run instance finds the files in my resources directory locally when I use
mvn clean spring-boot:run
but when I try to deploy to a cloud run instance it doesn't find the file. What's going on? Google's documentation is completely lacking and the examples don't cover this case. Not sure what's happening.
Obviously, the directory works because I have an application.properties file that's being read.
Folder structure
src
--main
----code
--resources
----application.properties
----kafka.properties
I also ran a func that traces the output of all the files in the dir for default "/" folder when trying to use a file and it just lists
myapp.jar
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
so I think it's pointing to the wrong folder but I'm not sure why or how to change it.
Related
I have a Spring boot project that also executes a node.js file at one point. My project structure is:
ROOT
- src/
- jsModules/
- script.js
At one point, I execute the script.js (with some parameters) from the java code using:
Process proc = Runtime.getRuntime().exec(executingScriptString);
Once I pack that into a fatJar i have to manually copy-paste the jsModules folder and the script.js to be in the folder root where the .jar is.
Is it maybe somehow possible to pack all the JS files into the fatJar so once I pack it up, I don't need to worry about JS files being copied.
I tried searching how to do this but didn't come across any answers.
Here is the answer:
Place the JS file inside the resources folder which makes it available on the classpath.
When executing the script make sure to adjust the path of the file. In my case it would be:
node src/main/resources/script.js.
Instead of the "/" symbol I'm using File.separator from java.io package and seems to be working correctly although on Windows it creates a string of node src\main\resources\pptrSoccer.js.
Hope it helps someone.
EDIT:
I tried using ResourceLoader (from Spring framework)
resourceLoader.getResource("classpath:script.js").getFile().getAbsolutePath();
It works while it's exploded (running from IDE) but when I pack to a fatJar it doesn't work.
I have one spring boot application which exposes some rest services.It contains some sql files and some python files which are stored in files inside resources folder in folder structure resources/scripts/.
Some of them are sql whereas some of them are python scripts.
I am creating an executable jar files using spring boot application. So when I package the application using maven, all those files go inside jar files as expected.Now, In a function inside code , I have a command as follow
Process p = Runtime.getRuntime().exec(run generator/generate.py)
generate.py is basically a python file containing stuffs to do.When I run the application within intellij , it works fine . When I package it and try to execute the jar file , it is not able to find the file and complaining .
Error running generator/generate.py: Cannot run program "generator/generate.py" (in directory "file:/Users/bpokharel/code/oprime_fm_service/target/oprime-fmservice-V1.jar!/BOOT-INF/classes!/scripts"): error=2, No such file or directory
failed to parse or execute scripts/commands.txt:45 [run generator/generate.py]
My resource folder is as follow:-
resources/scripts/commands.txt
resources/scripts/generator/generate.py
commands.txt contains other commands. One of the command is generator/generate.py. So I am basically reading commands.txt and executing each scripts as defined in commands.txt
I am reading commands.txt using inputstream and then executing each commands.
I believe Runtime.getRunTime tries to execute the command in a directory.
, but for executable jar file I am not sure how I can supply folder name where to execute the command
Any help would be appreciated .
Here is somewhat related but I have to go one step further than this .
Classpath resource not found when running as jar
I'm having trouble running the jar command in cygwin. The input-files parameter isn't treating the directory I'm passing it recursively when I'm referencing it with ".."s in my path.
For example, I'm running this in the same directory as the "src" directory. src/ contains my package structure of class and java files. This runs properly and creates a jar containing my source and class files.
jar cf jarname.jar src
However when I run this next command, I get an empty jar except for a manifest file.
jar cf jarname.jar localdir/../src
I need to run this from a script that needs to find this directory with a ".." directory so I need the 2nd command to work.
Anybody know why this isn't working or have a workaround? I tried using realpath but it complains that it can't find that path at all. I may be using it wrong though.
The Directory path in cygwin is different . To navigate to any drive for example to C drive we need to type in:
/cygdrive/c
A very easy work around i found useful is just to type cmd in the terminal . This allows you to use the actual path than the cygwin specific path .
try typing cmd and then running the command , it worked for me
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.
I have tomcat installed at "C:\Program Files\apache-tomcat-7.0.27"
I have eclipse installed at "C:\Program Files\eclipse"
And I have the workspace located at "C:\workspace"
I'm using "Java perspective", created a "Java Project" with the default output folder as "helloWorld/web/WEB-INF/classes".
The structure of the project goes like this:
-helloWorld
---src
-----servlets
-------hello.java
-------world.java
---web
-----WEB-INF
-------jsp
---------hello.jsp
---------world.jsp
-------lib
-------web.xml
---helloWorld.xml
---record.txt
doPost() in hello.java generate a random number, and write it to a text file "record.txt".
doPost() in world.java open the text file "record.txt" and read a number.
The system is working, but what I originally put in the record.txt file in eclipse project never get changed, and I'm sure that what world.java read from the file is exactly what hello.java generated.
I checked "C:\Program Files\apache-tomcat-7.0.27\work\Catalina\localhost\helloWorld", and only jsp files are there.
I then tried restart tomcat and reload and even undelopy and deploy again, but the previous generated number is still there. I didn't try restart computer.
My question is where is the record.txt file? It is definitely not the one in the eclipse project.
If you use a relative file path in your Java code then it will not be relative to your webapp it will be relative to where the process running Tomcat was started. Therefore you might find your file in the Tomcat bin directory or somewhere similar.
If you want to create a file relative to your webapp then you need to obtain the path to your webapp, which you can do by calling getServletContext().getRealPath("") in your Servlet.
Eclipse deployed projects are run in temp folder. the path looks some thing like this... tmp0/conf/catalina/localhost/projectname.....context.xml of servers might help you in this..
i suggest u should go for an absolute path....i faced same problem while using eclipse...That time i had to even provide the link to the user...
Check your webapps folder for a folder with the same name as your project.
C:\Program Files\apache-tomcat-7.0.27\webapps
Here you will find the exact folder structure and your record.txt file as well.
Hope this helps!