Cannot read file via docker container - java

I have a spring-boot application and I have some files placed inside /src/main/java/resources which I am trying to read it in my code. When the same code tries to read from docker container it says file does not exist. The same code works perfectly fine via localhost
The files are under /src/main/java/resources/data folder and this is my code which tries to read the file
private String getJson(String folderName, String id, StringBuilder sb) throws Exception {
String responseJson = null;
String filePath = "data" + File.separator + folderName + File.separator + id + ".json";
LOG.info("printing filePath : " + filePath);
LOG.info("printing id : " + id);
File f = new File(filePath);
// if(f.exists()){
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath)) {
LOG.info("printing inputStream : " + inputStream);
if (inputStream != null) {
responseJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
}
if (responseJson == null || responseJson.isEmpty()) {
LOG.info("json response is null : ");
throw new JsonNotFoundException(Constant.JSON_NOT_FOUND);
}
sb.append(responseJson);
} catch (IOException e) {
LOG.info("IO exception : ");
throw new IOException(e);
} catch (Exception e) {
LOG.info(" exception : ");
throw new Exception(e);
}
// }
// else{
// LOG.info("file doesnt exists : " + filePath);
// }
return sb.toString();
}
An example for the file path : src/main/resources/data/get-products/1420-17612-82.json
Docker file content
{
"commands":
[
"rm -rf .tmp",
"git clone git#github.com:{orgnname}/{test-service.git} -b COECP-973-Configure-logging-mechanism-for-Service .tmp/test-service",
"docker build .tmp/test-service/.docker/build/db -t local/test-service/db",
"docker build .tmp/test-service -t local/test-service/app"
]
}

So... you messed path for File and for resources from class path. What is the reason to have File f = new File(filePath);?
Here are things:
If you use File - files must be available to the JVM and as long as you use relative path like data\folderxxx\filexxx.json it must be available in container file system. I.e. data folder must be placed in image or mounted from outside exactly into directory from where JVM runs
If you use ClassLoader and ResourceAsStream root of your data directory must be defined in class path for JVM or be in Jar file - it is a root in classpath as well. Check your jar file - if data directory is in root of jar - all fine and files will be available by this.getClass().getClassLoader().getResourceAsStream(filePath), but not for new File(filePath)!
if not - make it happen or update your filePath for ResourceAsStream accordingly.

I have had faced the same issue earlier, though our requirement got more complex over time, but the following code should solve your problem:
ClassPathResource cp = new ClassPathResource("relative_path_to_file");
File f = null;
if (cp.exists())
f = cp.getFile();

Related

Create directory in Java but don't throw error if it already exists [duplicate]

The condition is if the directory exists it has to create files in that specific directory without creating a new directory.
The below code only creates a file with the new directory but not for the existing directory . For example the directory name would be like "GETDIRECTION":
String PATH = "/remote/dir/server/";
String fileName = PATH.append(id).concat(getTimeStamp()).append(".txt");
String directoryName = PATH.append(this.getClassName());
File file = new File(String.valueOf(fileName));
File directory = new File(String.valueOf(directoryName));
if (!directory.exists()) {
directory.mkdir();
if (!file.exists() && !checkEnoughDiskSpace()) {
file.getParentFile().mkdir();
file.createNewFile();
}
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(value);
bw.close();
Java 8+ version:
Files.createDirectories(Paths.get("/Your/Path/Here"));
The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists.
This code checks for the existence of the directory first and creates it if not, and creates the file afterwards. Please note that I couldn't verify some of your method calls as I don't have your complete code, so I'm assuming the calls to things like getTimeStamp() and getClassName() will work. You should also do something with the possible IOException that can be thrown when using any of the java.io.* classes - either your function that writes the files should throw this exception (and it be handled elsewhere), or you should do it in the method directly. Also, I assumed that id is of type String - I don't know as your code doesn't explicitly define it. If it is something else like an int, you should probably cast it to a String before using it in the fileName as I have done here.
Also, I replaced your append calls with concat or + as I saw appropriate.
public void writeFile(String value){
String PATH = "/remote/dir/server/";
String directoryName = PATH.concat(this.getClassName());
String fileName = id + getTimeStamp() + ".txt";
File directory = new File(directoryName);
if (! directory.exists()){
directory.mkdir();
// If you require it to make the entire directory path including parents,
// use directory.mkdirs(); here instead.
}
File file = new File(directoryName + "/" + fileName);
try{
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(value);
bw.close();
}
catch (IOException e){
e.printStackTrace();
System.exit(-1);
}
}
You should probably not use bare path names like this if you want to run the code on Microsoft Windows - I'm not sure what it will do with the / in the filenames. For full portability, you should probably use something like File.separator to construct your paths.
Edit: According to a comment by JosefScript below, it's not necessary to test for directory existence. The directory.mkdir() call will return true if it created a directory, and false if it didn't, including the case when the directory already existed.
Trying to make this as short and simple as possible. Creates directory if it doesn't exist, and then returns the desired file:
/** Creates parent directories if necessary. Then returns file */
private static File fileWithDirectoryAssurance(String directory, String filename) {
File dir = new File(directory);
if (!dir.exists()) dir.mkdirs();
return new File(directory + "/" + filename);
}
I would suggest the following for Java8+.
/**
* Creates a File if the file does not exist, or returns a
* reference to the File if it already exists.
*/
public File createOrRetrieve(final String target) throws IOException {
final File answer;
Path path = Paths.get(target);
Path parent = path.getParent();
if(parent != null && Files.notExists(parent)) {
Files.createDirectories(path);
}
if(Files.notExists(path)) {
LOG.info("Target file \"" + target + "\" will be created.");
answer = Files.createFile(path).toFile();
} else {
LOG.info("Target file \"" + target + "\" will be retrieved.");
answer = path.toFile();
}
return answer;
}
Edit: Updated to fix bug as indicated by #Cataclysm and #Marcono1234. Thx guys:)
code:
// Create Directory if not exist then Copy a file.
public static void copyFile_Directory(String origin, String destDir, String destination) throws IOException {
Path FROM = Paths.get(origin);
Path TO = Paths.get(destination);
File directory = new File(String.valueOf(destDir));
if (!directory.exists()) {
directory.mkdir();
}
//overwrite the destination file if it exists, and copy
// the file attributes, including the rwx permissions
CopyOption[] options = new CopyOption[]{
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES
};
Files.copy(FROM, TO, options);
}
Simple Solution using using java.nio.Path
public static Path createFileWithDir(String directory, String filename) {
File dir = new File(directory);
if (!dir.exists()) dir.mkdirs();
return Paths.get(directory + File.separatorChar + filename);
}
If you create a web based application, the better solution is to check the directory exists or not then create the file if not exist. If exists, recreate again.
private File createFile(String path, String fileName) throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(".").getFile() + path + fileName);
// Lets create the directory
try {
file.getParentFile().mkdir();
} catch (Exception err){
System.out.println("ERROR (Directory Create)" + err.getMessage());
}
// Lets create the file if we have credential
try {
file.createNewFile();
} catch (Exception err){
System.out.println("ERROR (File Create)" + err.getMessage());
}
return file;
}
A simple solution using Java 8
public void init(String multipartLocation) throws IOException {
File storageDirectory = new File(multipartLocation);
if (!storageDirectory.exists()) {
if (!storageDirectory.mkdir()) {
throw new IOException("Error creating directory.");
}
}
}
If you're using Java 8 or above, then Files.createDirectories() method works the best.

Command for WinRAR on specific file

I have a code to archive a ZIP file into another ZIP file, using command like this:
String rootftp = "C:\\ROOT_DIR_PUSHFILE\\";
String tampungString = "AAA\\PFILE\\AAA20140531.zip";
String password = "testing";
String command = "cmd.exe "
+ "/C"
+ " cd C:\\Program Files\\WinRAR \n"
+" && rar a -n "+rootftp.trim()+tampungString+".zip"+" "+ rootftp.trim()+tampungString + " -p"+password.trim();
System.out.println(command);
File file = new File(rootftp.trim()+tampungString);
try {
Runtime.getRuntime().exec(command);
file.delete();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
But the result is :
When I try to archive a specific file, the results is always archive from root directory, not just the specific file, like AAA20140531.zip file, can you explain, why? And how to fix it?
Use this command to create .rar without parent folder:
rar a -ep1 c:\ROOT_DIR_PUSHFILE\AAA\PFILE\AAA20140531.zip c:\ROOT_DIR_PUSHFILE\AAA\PFILE\ -p123456
-ep1 switch makes the result to exclude the base folder from the paths,

Can't delete file in java on windows ....?

I am trying to use file.delete() but it does not delete file ? I tested it on linux it delete files but on windows it does not delete file why..?
code :
private File getFiletobeUpload(File foto) {
boolean errorRename = true;
File uploadFile = null;
File testFile = foto;
String subdirname = this.checkDir(foto);
if (testFile.canWrite()) {
uploadFile = new File(AppConstants.PHOTOPATH + "/" + subdirname + "/" + testFile.getName());
try {
FileInputStream origStream = new FileInputStream(testFile);
FileOutputStream outStream = new FileOutputStream(uploadFile);
origStream.getChannel().transferTo(0, testFile.length(), outStream.getChannel());
origStream.close();
origStream = null;
outStream.close();
outStream = null;
} catch (Exception e) {
this.errorString += "error while writing to orig dir";
logger.error(e);
}
errorRename = !testFile.delete();
if (errorRename) {
this.errorString += "error while deleting the file";
}
}
testFile = null;
return uploadFile;
}
your code seems a bit odd, you are checking "testFile" for write access but then actually reading from it (FileInputStream). Is the first try/catch block running without exception? Maybe you should check if the file even exists:
System.out.println("File exists: "+testFile.exists());
errorRename = !testFile.delete();
Also if you just want to rename a file, use:
file.renameTo(File dest)
A general suggestion: consider using the classes in the java.nio package to your file IO if you can (not sure which version of Java you're running with) because the error handling is improved and you should be able to find a more specific reason for the failure based on the type of exception thrown and the exception message.
As per API, the disk-drive specifier - "/" for the UNIX root directory, and "\\" for a Microsoft Windows UNC pathname.
so for windows:
uploadFile = new File(AppConstants.PHOTOPATH + "\\" + subdirname + "\\" + testFile.getName());

Create A File in the WebContent Folder From Java

I wanted to know how to create a file in the WebContent folder of a dynamic web project using Java?
The basic question remaining is how to get the path of the WebContent folder.
Note: No servlet is to be used!
Edit:
Okay, i am trying to create a new xml file from java method. I want the file to be created in the WebContent folder so that the file is created even when the application is deployed.
I am using Jboss, maven, JSF to create the dynamic web project. I need the xml file to pass data to highcharts. Please note that i will be using this method only.
Overview:
Create xml file on request
XML file to created in the WebContent folder itself
Use this xml file to pass data
Glassfish solution.
AbstractSearchPageBean - any of your class
private static final String WEB_INF = "WEB-INF";
public static String getWebPath() {
final String webInfPath = getWebInfPath();
return webInfPath.substring(0, webInfPath.indexOf(WEB_INF) - 1);
}
public static String getWebInfPath() {
String filePath = "";
java.net.URL url = AbstractSearchPageBean.class.getResource("AbstractSearchPageBean.class");
if (url != null) {
String className = url.getFile();
filePath = (className.contains(WEB_INF)) ? className.substring(0, className.indexOf(WEB_INF) + WEB_INF.length()) : className;
}
return filePath.replace("%20", " ");
}
// Create file in webapp/xml directory
private void createXmlFile(String xml) {
try {
String fileName = System.currentTimeMillis() + ".xml";
File file = new File(Settings.getWebPath() + File.separatorChar + "xml" + File.separatorChar + fileName);
logger.debug("parseXML(): Creating file: " + file.getAbsolutePath());
if (file.createNewFile()) {
FileWriter fw = new FileWriter(file);
fw.write(this.parseXML(xml));
fw.flush();
fw.close();
logger.debug("parseXML(): file saved to the: " + Settings.getAPPLICATION_DOMAIN() + '/' + fileName);
} else {
logger.warn("parseXML(): Can't create file: " + file.getAbsolutePath());
}
} catch (IOException ioe) {
logger.error("parseXML(): Bad save file: ", ioe);
}
}

How to Declare Folder Path?

I have a desktop application using Swing library. Application is running a batch file. So I created a lib folder in main project directory and put batch file in it. To run this, I am showing lib\a.exe to run this. It is working on my laptop. I exported .jar and put lib folder next to it. It is working on my laptop, but not working on some other laptops. How to fix this?
Error message is: Windows cannot found lib\a.exe.
String command = "cmd /c start lib\\a.exe";
try {
Runtime.getRuntime().exec(command);
increaseProgressBarValue();
} catch (IOException e) {
e.printStackTrace();
}
You need two things:
find the directory of the jar file of your application
call a.exe with the correct working directory
You can get the location of the jar with the getJar method below:
private static File getJar(Class clazz) throws MalformedURLException {
String name = clazz.getName().replace('.','/') + ".class";
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource(name);
System.out.println(url);
if (!"jar".equals(url.getProtocol())) {
throw new MalformedURLException("Expected a jar: URL " + url);
}
String file = url.getPath();
int pos = file.lastIndexOf('!');
if (pos < 0) {
throw new MalformedURLException("Expected ! " + file);
}
url = new URL(file.substring(0, pos));
if (!"file".equals(url.getProtocol())) {
throw new MalformedURLException("Expected a file: URL " + url);
}
String path = url.getPath();
if (path.matches("/[A-Za-z]:/")) { // Windoze drive letter
path = path.substring(1);
}
return new File(path);
}
To call lib\a.exe, you can do something like this:
File jar = getJar(MyClass.class); // MyClass can be any class in you jar file
File dir = jar.getParentFile();
ProcessBuilder builder = new ProcessBuilder();
builder.command("lib\\a.exe");
builder.directory(dir);
...
Process p = builder.start();
...
Maybe you have to try if this folder lib exists and if it doesn't than create it with
file.mkdir();
This is a just a checking. But your filepath must be like this ../lib/a.exe.

Categories