I am creating an application for Android which enables users to create encrypted LUKS partitions and then mount them to given directories on external memory.
For the partition to be usable I create an ext2 file system using the Busybox mkfs.ext2 command. The problem occurs once a user tries to create a file/directory at the root of the partition. For some reason it is impossible to create a file through Java as the "File.mkdirs()" method fails. However, it is possible to create this file through the command-line. And this error occurs only when at the root of the partition (i.e if I create a folder through the command line I am then able to create files within that folder through Java). Also, I am able to create a file if I create a vfat file system instead of ext2.
Any help would be greatly appreciated.
Harry
EDIT
Fixed. I was mounting the file system as root
My final solution to this problem was to create a vfat file system rather than ext2 as vfat doesn't have permissions etc... This worked for me as I did not need the extra security of permissions. However, if you need an ext2 file system which you need to mount as root but want it to be useable by other users I recomment looking into the mount ownmask option (man mount).
Related
I have a Spring Boot application that wraps the Eclipse BIRT reporting tool and runs as a Windows service via winsw. When a report with a chart generated by the engine is rendered, Spring Boot saves the image file in a folder it creates in the Windows/Temp directory. This folder and its contents persist across startups, which is less than desirable.
The application depends on a data folder that exists in a predefined location that exists for all installs of the software package. Given that, the ideal situation would be to create the folders in the package's data folder where it can be managed easily. Is there any way to accomplish this (preferably a method that allows the embedded Tomcat server to find and serve the files)?
Edit: Updated with results of response by #Magnus
The images in question are generated by the ReportEngine render process, so I think it's a BIRT thing not a Tomcat thing, but just to be sure I added server.tomcat.basedir=path/to/directory to my application.properties file. Still writes to Windows/Temp.
However, this reply got me thinking and i found that BIRT's EngineConfig has a setTempFolder method. Setting that to point at the desired folder (with the server.tomcat.basedir value in the properties file) results in BIRT doing work in the temp folder, but the generated images are still saved to the Windows\Temp directory.
Edit: Update 2
I was able to set the birt.viewer.working.path property in the EngineConfig and confirm that it was set by retrieving the value from the ReportDesignHandle (or maybe the ReportRunnable). With the changed value, the ReportEngine now does its work in the right directory, but still puts images into the Windows\Temp folder. Overriding the java.io.tmpdirproperty did nothing.
Edit: Update 3
This kind of works.. Setting the image folder location in the HtmlRenderOption results in the application not creating a folder for generated images in Windows\Temp (yay!). However, it is not putting the images in the location specified in the HtmlRenderOption, either (boo!), so i'm not really sure where to look so such image files can be cleaned up periodically.
Try to set imageDirectory in HTMLRenderOption.
HTMLRenderOption options = new HTMLRenderOption();
options.setImageDirectory("path/to/image/directory");
...
renderTask.setRenderOption(options);
see http://www.eclipse.org/birt/documentation/integrating/reapi.php
The embedded tomcat defaults to creating a directory in the temp directory specified by the java.io.tmpdir system property.
You can manually set the tomcat temp dir with the application property server.tomcat.basedir
Looking at the codebase there are a few places it deals with creating temp files.
From what I can gather the main place that the temp image directory is set si the ParameterAccessor class.
The logic is fairly complex but, it appears to default to ${birt.viewer.working.path}/report/images.
If the working path system property is not set, or the directory is not writeable then it will default to the java.io.tmpdir directory.
I would try setting the system property birt.viewer.working.path, make sure it is writeable.
If that doesnt work you might have to resort to overriding the java.io.tmpdir system property.
I'm starting to build a JFrame application to work with File Handling. What I'm trying to get done from the application is that
it reads the contents of all the texts files in a particular location and merges the contents & creates one single text file.
The main property this application should have is that it should not have the navigate-to-location feature. Suppose if I paste this application in location C:\Users\Desktop\application.exe, the application must search the location for all the text files (i.e. on Desktop) & merge them into one single text file.
I've observed this in patch tools to patch softwares, they never ask for location for the software's_launcher.exe, they just tell us to paste the patch in the directory where the launcher belongs.
How do they do it? How can I do the same for my own application?
"./" is to specify current directory.
if you use
File f1 = new File("./");
then f1 is reference of current directory.
if your application is at C:\Users\Desktop\application.exe place then all files & folder at C:\Users\Desktop can access by "./" string
If my application is run by root user, creating log file by root. So, if the app is run by another users, display 'permission denied'.
There are the logs in /opt/myapp/logs/ .
I want to set permission for log file of log4j programmatically (May be using log4j configuration) (All user should write the log file. To permit as chmod 766). Is it possible?
Writing logs to single folder will require to manually set log file name and change log folder permissions.
If you need to run your application from many users you will likely should use user home (or sub folder) to write logs.
According to the Filesystem Hierarchy Standard, logs must be put in /var/log. You can avoid clutter this folder by creating a /var/log/myapp folder, where you'll put your logs. Ensure the folder and its contents are chmod 666, or better, 766.
This is the standard used by Apache, for example.
I am using tomcat server for my java application(Servlet,jsp).In my servlet page calling one java class function.Actually the java file is written separately and i will call the function written inside it.With in the function, i have to read one config(user defined) file for some purpose.so, i am using File class for that.
But, here i have to give relative path of the config file(user defined).Because, now i am running this application in local windows server.But my live server is based on Linux.So, the file path is changed in linux.
File f1=new File("D:\tomcat\webapp\myapp\WEB-INF\src\point_config.txt"); -- windows
File f1=new File("D:\ravi\tomcat\webapp\myapp\WEB-INF\src\point_config.txt"); -- linux
So, i have to give relative path of the file that is common to both windows and linux machine.
Is there a way to do this?
Please guide me to get out of this issue?
Place your config file under your webapp WEB-INF/classes folder and read like this in code
InputStream is=
YourClassName.class.getResourceAsStream("point_config.txt");
The path of the config file leads into the WEB-INF folder
tomcat\webapp\myapp\WEB-INF\src\point_config.txt
Anything inside WEB-INF is protected and cannot be user-defined once the web application has launched. If you meant to read from a user-defined configuration file from the file system, please use an API like the common configuration API.
If you want to insist on keeping the file inside the WEB-INF folder, use the Class.getResourceAsStream() method to obtain the configuration instead. That would not make the configuration user-defined though.
I want to create an ini file to store some settings for my application. Is it a good idea to find where the jar file is located and create an ini file there? If yes, then how can I find the location of the jar file?
But if you know a better solution for something like this, I would like to hear some of them.
EDIT: I'm using mac and I want to run the same application in windows. I could write something in the System.getProperty("user.home") directory, but I want to keep the system clean, if the user decides to remove the app. There is no a better way to store the settings file, for example in the same directory with the application?
You can locate your application directory using the ClassLoader. See: Java: finding the application directory. Rather than an .INI file, use a .properties file - you can load and save this via the Properties class.
As others have noted, you should not write user settings to your application directory. What if the user does not have write access to the application directory? What if your application is being used by multiple users on the same system at the same time? Neither of these situations are unusual, even on Windows.
You might still want to load some settings from the application directory - perhaps the administrator has configured default settings there.
A common convention is to save user settings to the user's home directory:
/home/user/.eclipse
C:\Documents and Settings\User\.eclipse
Although this means you might leave stray files behind, this can be beneficial if the user re-installs the app. Document such things in a README. Here is how to create and get a reference to the directory:
public static File getSettingsDirectory() {
String userHome = System.getProperty("user.home");
if(userHome == null) {
throw new IllegalStateException("user.home==null");
}
File home = new File(userHome);
File settingsDirectory = new File(home, ".myappdir");
if(!settingsDirectory.exists()) {
if(!settingsDirectory.mkdir()) {
throw new IllegalStateException(settingsDirectory.toString());
}
}
return settingsDirectory;
}
On unix-like operating systems, starting the directory name with a period (".myappdir") will make the directory hidden. On Windows, it will be located below My Documents, so users will not see the directory unless they go looking for it.
If the settings are only written by your application (rather than edited manually), consider using the Preferences API.
You should not be storing temp files in the install directory of an application. Remember, the user running the application may not have write access to that directory. The safest place to put stuff like that is in C:\Documents and Settings\username\Application Data\ApplicationName folder (adjusting the name as necessary).
That said, however, I would probably store that type of stuff in the registry instead of a file on their computer. (But, that's just me.)
Typically Java programmers don't use .ini files, but .properties files (different format). You can use the java.lang.Properties class as a nice programmatic wrapper if you do.
While you can get the location of your jar file by calling getProtectionDomain().getCodeSource().getLocation() on your class's .class member, I do not recommend that you do this.
I would instead write the file to the System.getProperty("user.home") directory - the users' home directory, or if it is truly temporary, System.getProperty("java.io.tmpdir")
It depends whether your ini needs to be human readable/writable under normal circumstances. If not, you can use a properties file rather than an ini file, and store it in the "user" directory.
As for finding the jar file, you would have to find the ClassLoader for a class known to be loaded from the jar, check that it was the appropriate type of ClassLoader (ie that it's really been loaded from a jar), and you can extract the path from that. I can probably dig out the code to do this if that's really what you want. I wouldn't necessarily recommend it.
EDIT The user.home property will give you the user directory, which you can safely use.
The idea with the .properties file instead of the INI file is good. Also, if you store some sensitive data in there, you may consider encrypting it. Check this out:
https://www.owasp.org/index.php/How_to_encrypt_a_properties_file
or this:
encrypt and decrypt property file value in java