unable to access file in resources/static folder spring-boot - file

I have a file in a location:
/resources/static/fcm-admin
It's absolute path: /home/jitu/project-name/src/main/resources/static/fcm-admin
I have tried to access this file in the following ways
val file = ResourceUtils.getFile("classpath:fcm-admin")
It gives me an error
java.io.FileNotFoundException: class path resource [fcm-admin] cannot be resolved to an absolute file path because it does not exist
I have tried to access the file in various ways but it is not working. I just want to the access file fcm-admin without giving the full absolute path. Anything will be helpful
EDIT:
So I'm able to access the file on local with the below code -
val file = ResourceUtils.getFile("classpath:static/fcm-admin")
But I'm not able to access it on the production server. And I'm getting below exception
class path resource [static/fcm-admin] cannot be resolved to an absolute file path because it does not reside in the file system: jar:file:/var/app/current/application.jar!/BOOT-INF/classes!/static/apple-app-site-association

You forgot to add static in the path
val file = ResourceUtils.getFile("classpath:static/fcm-admin")
EDIT because of comment
Load your file from Classpath:
val file = this.javaClass.classLoader.getResource("/static/fcm-admin").file;
When you load a resource using the class loader it will be start in the root of your classpath.

Local server can work with ClassPathResource but will fail on production
To solve error on production, change your code to
import org.springframework.core.io.ResourceLoader;
import java.io.InputStream;
import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.ByteArrayResource;
import org.apache.commons.io.IOUtils;
#Autowired
private ResourceLoader resourceLoader;
InputStream logoFileStrem = resourceLoader.getResource("classpath:static/images/image.png").getInputStream();
InputStreamSource byteArrayResource = new ByteArrayResource(org.apache.commons.io.IOUtils.toByteArray(logoFileStrem));

I spent so many time, lot of code work on dev but not on prod.
I used SpringBoot with war in production.
To load a file in resources/static the only solution who work in dev and in prod :
val inputStream = Thread.currentThread().contextClassLoader.getResourceAsStream("static/pathToTourFile/nameofFile.extension")
val texte :String = inputStream.bufferedReader().use(BufferedReader::readText)

Related

My service is not recognizing the file names contained in directory with Java 8

I am creating a service and I am using java 8, I want to retrieve the file names as a list, those are in a folder into resources/ and when I am running locally is working but when I deployed my application into DEV/QA environment is getting empty, is not recognizing those files, and I have some considerations:
In the full path, in one folder is hidden folder (.cache), that
could affect it?, this is the path:
file:/newarch/apps/project-1/servers/project-service-1/workarea/org.eclipse.osgi/55/data/cache/com.ibm.ws.app.manager_723/.cache/project-service-ser.jar!/services
I gave rights on the folder as reader and writer.
This is the structure of the folder that I wanna retrieve the names:
resources/
services/
service-host1.js
service-host2.js
service-host3.js
service-host4.js
This is my code to read:
public List<String> getFilesFromDirectory(String dirName) throws IOException {
dirName = dirName + "/";
try (Stream<Path> pathStream = Files.list(Paths.get(dirName))) {
return pathStream.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toList());
}
}
this is the way how I am getting the full path:
Thread.currentThread().getContextClassLoader().getResource("service").getPath()
Could you give any advice what could be the problem?

How to add additional files in Sagemaker Pipeline Processing Step

I want to have additional files which can be imported in preprocess.py file
but i am not able to import these directly.
My directory looks like this:
Want to import from helper_functions directory into preprocess.
I tried to add this in setup.py file but it did not work.
package_data={"pipelines.ha_forecast.helper_functions": ["*.py"]},
One thing which kind of worked was to add this folder in input like this:
inputs = [
ProcessingInput(source=f'{project_name}/{module_name}/helper_functions',
destination="/opt/ml/processing/input/code/helper_functions"),
]
But this was putting the required files in some other directory which I was not able to import anymore.
What is standard way of doing this?
You have to specify the source_dir. Within your script then you can import the modules as you normally do.
source_dir (str or PipelineVariable) – Path (absolute, relative or an
S3 URI) to a directory with any other training source code
dependencies aside from the entry point file (default: None). If
source_dir is an S3 URI, it must point to a tar.gz file. Structure
within this directory are preserved when training on Amazon SageMaker.
Look at the documentation in general for Processing (you have to use FrameworkProcessor and not the specific ones like SKLearnProcessor).
P.S.: The answer is similar to that of the question "How to install additional packages in sagemaker pipeline".
Within the specified folder, there must be the script (in your case preprocess.py), any other files/modules that may be needed, and also eventually the requirements.txt file.
The structure of the folder then will be:
BASE_DIR/
|─ helper_functions/
| |─ your_utils.py
|─ requirements.txt
|─ preprocess.py
Within your preprocess.py, you will call the scripts in a simple way with:
from helper_functions.your_utils import your_class, your_func
So, your code becomes:
from sagemaker.processing import FrameworkProcessor
from sagemaker.sklearn import SKLearn
from sagemaker.workflow.steps import ProcessingStep
from sagemaker.processing import ProcessingInput, ProcessingOutput
BASE_DIR = your_script_dir_path
sklearn_processor = FrameworkProcessor(
estimator_cls=SKLearn,
framework_version=framework_version,
instance_type=processing_instance_type,
instance_count=processing_instance_count,
base_job_name=base_job_name,
sagemaker_session=pipeline_session,
role=role
)
step_args = sklearn_processor.run(
inputs=[your_inputs],
outputs=[your_outputs],
code="preprocess.py",
source_dir=BASE_DIR,
arguments=[your_arguments],
)
step_process = ProcessingStep(
name="ProcessingName",
step_args=step_args
)
It's a good practice to keep the folders for the various steps separate for each and don't create overlaps.

Image Extractor by AI Habitat produces a configuration error when importing Matterport dataset

I need help understanding the error message, which is along the lines of changing the file name to json because the configuration fails. I have a long error message but pasted the part that is mostly repeated throughout the message:
/Users/kyra/Documents/GitHub/habitat-sim/matterport/scans/house1/8194nk5LbLH 13/poisson_meshes/8194nk5LbLH_10.stage_config.json
I0412 19:04:17.735939 42397184 AttributesManagerBase.h:296] AttributesManager::createFromJsonOrDefaultInternal (Stage) : Proposing JSON name : /Users/kyra/Documents/GitHub/habitat-sim/matterport/scans/house1/8194nk5LbLH 13/poisson_meshes/8194nk5LbLH_10.stage_config.json from original name : /Users/kyra/Documents/GitHub/habitat-sim/matterport/scans/house1/8194nk5LbLH 13/poisson_meshes/8194nk5LbLH_10.ply | This file does not exist.
I0412 19:04:17.736085 42397184 AbstractObjectAttributesManagerBase.h:182] AbstractObjectAttributesManager::createObject (Stage) : Done making attributes with handle : /Users/kyra/Documents/GitHub/habitat-sim/matterport/scans/house1/8194nk5LbLH 13/poisson_meshes/8194nk5LbLH_10.ply
I0412 19:04:17.736093 42397184 AbstractObjectAttributesManagerBase.h:189] File (/Users/kyra/Documents/GitHub/habitat-sim/matterport/scans/house1/8194nk5LbLH 13/poisson_meshes/8194nk5LbLH_10.ply) exists but is not a recognized config filename extension, so new default Stage attributes created and registered.
I0412 19:04:17.736124 42397184 SceneDatasetAttributes.cpp:46]
What I did: Ran image extractor after activating Conda env. I modified the image extractor to change the file path to point to a .ply file in the matterport dataset.
Setup: 1)Facebook's AI Habitat-sim built from source,
2)MacBook Air M1,
3)Conda environment with the dependencies (using pip install -r requirements.txt) but habitat-sim is not installed by Conda,
4)Matterport3D dataset (downloaded one house).
Thank you.

React Native- How to access .csv files in my project hierarchy

I am trying to load a CSV file in my js class and I am unable to do so in react native. This file is available locally. Not downloaded. Whenever I give a path to the CSV file, I get an error that says The module could not be found. No such module exists. I have tried placing the CSV in various folders and also at my project root level. It does not work. I noticed images do not face the same problem.
I have even tried doing this.
https://willowtreeapps.com/ideas/react-native-tips-and-tricks-2-0-managing-static-assets-with-absolute-paths/
Again it works for images but strangely not for CSVs.
I have tried the import statement, require statement and even relative path for the files. Same error every time.
I am new to react maybe I am missing some step?
EDIT: Two of the ways I tried
import RNFS from 'react-native-fs';
import Papa from 'papaparse';
import CSVData from './CSVData.csv';
function loadAllCSV()
{
console.log('Loading CSV');
var path = './CSVData.csv';
console.log(path);
const fileContents = RNFS.read(path);
console.log('File Data ' + fileContents);
Papa.parse(CSVData, {
download: true,
delimiter: '\t',
complete: function(results) {
console.log('ZOMBIIIIEEEE');
console.log(results);
}
});
}
Error:
Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(Unable to resolve module ./CSVData.csv from /Users/abc/xyz/SearchPage.js: The module ./CSVData.csv could not be found from /Users/abc/xyz/SearchPage.js. Indeed, none of these files exist:
/Users/abc/xyz/CSVData.csv(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)
/Users/abc/xyz/CSVData.csv/index(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json) (null))
Adding the .CSV files to the project assets bundle in iOS fixed this problem for me.
Use Xcode to place the files in the project in the Project Settings under the Build Phases Tab check if the Assets are included in the Copy Bundle Resources Section. Add the files to the list here incase they are missing.

problem with loading a jar file which has dependency to another jar file

I have a problem while loading my jar file at run time.
My hotel.jar is loaded and a method of it (makeReservation) is invoked using the following code:
File file = new File("c:/ComponentFiles/hotel.jar");
URL jarfile = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/");
URLClassLoader cl = URLClassLoader.newInstance(new URL[]{jarfile});
Class componentClass = cl.loadClass("HotelPackage.HotelMgt");
Object componentObject = componentClass.newInstance();
Method setMethod = componentClass.getDeclaredMethod("makeReservation", null);
setMethod.invoke(componentObject, null);
The problem is in the class HotelPackage.HotelMgt of my jar file , I have a class variable of another class (HotelPackage.Hotel) which is in another jar file.
I tried to open and load the other jar file with the same code as above but I receive the exception that cannot find the class def.:
Exception in thread "main" java.lang.NoClassDefFoundError: BeanPackage/Hotel
what is the solution?
You can specify dependencies between JARs by defining the Class-Path property in the JARs' manifest files. Then the JVM will load the dependency JARs automatically as needed.
More information is here: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html
Thanks, but I found another solution that really works. Since I know whole the component series that are going to work with each other, I load them all with one class loader instance (array of URLs). then the classloader itself manages the dependencies.

Resources