I am new in Netbeans, I am doing project which is manipulate with MS Access Database (.mdb or .accdb).
Inside my code, I need to write the whole directory of my database file in order to connect it, like:
conn = DriverManager.getConnection("jdbc:ucanaccess://D:/abc/def/db.accdb");
Which folder should I put my database file in so that I no need to write the whole directory to connect it, like: conn = DriverManager.getConnection("jdbc:ucanaccess://db.accdb");?
You do not want to hardcode a database path in your code: good point!
Unfortunately, it looks like UCanAccess offers no special folder to automagically find the database: it just tries to find it where it is declared. So if you use a relative path, it will rely on Java processing and Javadoc for File class says:
By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
If you use shortlinks under window, you can specify a start directory in the shortlink, but I would not rely on it because it is not a common usage even on Windows.
So the correct way (and common usage) is to use an environment variable. This variable would contain the database fullpath if you have no other external configuration value, or it will contain the path of a property file that in turn contains other configuration values.
Related
I have read that files within a jar file are not supposed to be modified, and I think that explains why getting an outputStream is not as simple as getting an inputStream calling getClass().getResourceAsStream() to read a file. When using properties files values are going to be retrieved during execution, but what if those values are subject to change? It would be tedious to recompile the program.
In my case those values are IP address, username and a path to an external file. So I guess I could not move the external file even by accident because my properties file in the jar have a different path to that file. Is this the way is supposed to be or is there a more flexible way that I am not aware of?
Usually property files are located outside of jar somewhere on production system's classpath. Values are usually changed once in a while via provisioning scripts / manually and then only application restart is required, not a re-compile.
So, to sum it up, I'd suggest not having properties file inside the jar.
I need to read in config.properties for some configuration.
I am open to any way of doing it, with requirements.
I want it outside my war file so that it can be changed easily, and does not require a rebuild.
I have searched everywhere but cannot find how to do it, I'm sure this is java 101, but I cant figure it.
I have tried classloader but that seems to only load resources from inside the war, and I cannot find how to get the location I loaded the war into my server from to read it from there.
I also cannot find a way to pass in an argument via command line parameter's as its a soap endpoint, which I can access anywhere in my code?
I saw this Where to place and how to read configuration resource files in servlet based application?
and i want to use the file system approach but I don't want a hard coded path for the config file.
I just want something simple and easy, I know there is something but I just cannot find it.
use this this is helpful your question . i think.
In one method abcd
public static String abcd(String one) {
properties = new Properties();
properties.load(<classNmae>.class.getClassLoader().getResourceAsStream("AppResources.properties"));
return properties.getProperty(one);
call this code
String fileLocation = abcd("internal property file");
Properties properties = new Properties();
FileInputStream fis = new FileInputStream(fileLocation );
properties.load(fis);
fis.close();
acd = (String)properties.get("acd");
Note:
AppResources.properties have external file location D:/aaa.properties file
then in second method you read properties of external file
I want it outside my war file so that it can be changed easily, and does not require a rebuild.
May not be how you want it, but just to answer this, we have a similar scenario where our web applications are deployed in web servers located in locations /apps/servers/webserver-1, /apps/servers/webserver-2. And we have properties placed in some other locations like /apps/my-web-app1/app.properties and /apps/my-web-app2/app.properties.
Now good thing about this structure is that if I need to update any property, I just do the edit in the relative property and restart my web server. A downside of this is that I have to pass in paths to these properties file as system arguments to my web-server startup scripts (in my case, these are the catalina.sh files, yes I am using tomcats).
So my catalina.sh has line somewhere lying around something like
export JAVA_ARGS `-Dpath.to.properties.of.my-web-app2=/apps/my-web-app2/app.properties` ....
To read these properties, I have a Property Utility function that gets called by the StartupServlets of each application. The purpose of this function is to simply open up this file by reading the system property path.to.properties.of.my-web-app2 and puts these properties in something like a cache (a HashMap in my case) from where I can access them easily throughout the application.
Hi I was wondering how to access files in the project folder in java/maven, i have thought about using src/main/resources, but i have been told it is a bad idea to write to files in this directory, and should only be used in configuration for the project, So i have created a new non source folder but i was wondering if there is a way to access this file in java without giving an absolute path, as i need to use it in different env. any help or other suitable suggestions would be great here thanks. i will be writing to the files at runtime.
EDIT:
i am using this to acces the file:
private static final String ACTUAL_VALUES ="verification/actualCounterValues.csv";
where verification is a folder i have created in my project
As i understand you, your goal is to access a file from within your app, without hardcoding a relative path because you're going to run it on different environments.
The first thing you may have to solve is to decouple the file-reference from your app - because if you move your app it must adapt to the new environment.
You may solve that by putting an absolute file-reference into the system-environment (which is accessible through the System.getenv() method). Another way could be to deliver the file-path as command-line-argument.
After that you have to specify when which running instance of your app will access the file. If your application runs separated in the maven and a production environment - everything is fine with using relative paths in the maven-project. The production-app will then generate and use its own file in its environment.
If they must share the file, you have to provide physical access from both environments to that file-path, after that you may access that file with separate absolute paths (delivered through cli-args or system-properties) or with "hard coded" relative paths, which access a file-link (which point to that absolute file).
But i must discourage you from using any hardcoded configuration-specific variables for ongoing maintenance reasons.
And if these two application-instances will access both one single file, you should also be aware of possible concurrency difficulties (especially in the filesystem - see that post).
Is there a way of accessing the current script's absolute physical path via a variable/property? There doesn't appear to be anything listed via a Debug Sampler.
It's incredibly annoying that actions like loading CSV files and JMX Includes uses the current working directory as its relative path.
I used the answer provided by haridsv. It worked great except that I needed to put the directory to the JMX file in a variable. I made a "User Defined Variables" component and used BeanShell in the variable's "Value" field like this:
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}
The first BeanShell section calls the Java class that gets the directory in question. The second appends a file separator to the path, which is of course optional.
Include Controller
As per component's reference:
This element does not support variables/functions in the filename
field.
However, if the property includecontroller.prefix is
defined, the contents are used to prefix the pathname. If the file
cannot be found at the location given by prefix+filename, then the
controller attempts to open the fileName relative to the JMX launch
directory (versions of JMeter after 2.3.4).
You can pass JMeter a java property named includecontroller.prefix
which can be used to prepend a directory to the JMX file you're
including.
1) In case of console launch use:
-Jincludecontroller.prefix=/full/path/to/jmx/scripts/dir/
2) in case of GUI - add the same to .sh/.cmd/.bat file or write a wrapper file;
3) in case of Jmeter Ant Task usage - set as separate property:
<jmeter
jmeterhome="${jmeter.home}"
testplan="..."
resultlog="...">
<property name="jmeter.save.saveservice.assertion_results" value="all"/>
<property name="jmeter.save.saveservice.output_format" value="xml"/>
<property name="includecontroller.prefix" value="..."/>
</jmeter>
CSV Data Set Config
As per component's reference:
Relative file names are resolved with respect to the path of the
active test plan.
Absolute file names are also supported, but note
that they are unlikely to work in remote mode, unless the remote
server has the same directory structure. If the same physical file is
referenced in two different ways - e.g. csvdata.txt and ./csvdata.txt - then these are > > treated as different files. If the OS does not distinguish between upper
and lower case, csvData.TXT would also be opened separately.
You can declare a test plan variable that retrieves parameter value with the folder containing csv data files:
e.g.csv.path | ${__P(csv.path, ${__property(user.dir)}${__BeanShell(File.separator,)})}
CSV Data Set Config
Filename = ${csv.path}${__P(users-list,)}
Setting from console:
-Jcsv.path=/full/path/to/csv/data/dir/
Setting for distributed testing setup:
-Gcsv.path=/full/path/to/csv/data/dir/
By saying "current script's absolute physical path", I am guessing OP is referring to the location where the testplan (jmx file) is loaded from. I needed exactly this to generate a CSV file from BeanShell script at the beginning of the run, which is subsequently used in a CSV Data Set Config to read back, so I wanted the script to work just like how the later works when no path is specified. I went through the JMeter source and found this working solution:
import org.apache.jmeter.services.FileServer;
log.info(FileServer.getFileServer().getBaseDir());
I tested this and saw the correct path in the jmeter.log.
My particular issue was that my relative Include Controller path included a backslash which broke on Linux and OSX.
The solution was to use a forward slash in relative paths, which works on all platforms.
I'm newbie to java.
I have some directory structure
product/
conf/
classes/com/../..
conf/ contains some configuration file, while under classes/ I have my application.
How can I ensure from inside java code that I'm able to find file in conf/ despite way I'm executing it (e.g. from eclipse, from different directories, from crontab etc.).
P.S.
Files in conf/ are not resources, since required to be edited by user.
Is there're way to know where my .class, so I canuse relative path form that directory to reach my directory (e.g. MY_CLASS_DIR/../../../../conf)
I would put the conf directory into the class path. That way you can always find them by:
YourClass.class.getClassLoader().getResource("conf/....");
You can use the absolute path, including the way to product.
Or you may use a configuration setting, by starting your program like
java -DXY_HOME=/some/path/product ...
From the javacode, you use it:
String xyHome = System.getProperty ("XY_HOME")
Or you use a kind of inifile in your home directory, where you specify where to look for the conf-directory.
Rereading your question multiple times, it is unclear to me what your goal is. To find the conf dir independently from where you are (eclipse, crontab, ...)? But the headline asks for the CWD, which is the opposite - the directory, depending on where you are.
Both is possible, but you have to decide what you want.
Its safe to use relative paths than absolute paths. Even if you JAR your classes tomorrow it will work as is,
Put you configuration files in classpath during deployment.(Please note that
project directory structure can be different from that of deployment directory structure)
product/
classes/com/../..
classes/conf/some_conf.properties
Then you can use Apache common configuration to get the URL of file
URL urlOfFile = org.apache.commons.configuration.
ConfigurationUtils.locate("conf/some_conf.properties");
The other alternative you can try is,
URL urlOfFile = <SomeClassFromClassesFolder>.class.
getClassLoader().getResource(resourceFile);
Once you get the URL of your configuration file getting stream out of it very simple,
InputStream stream = urlOfFile.openStream();
Good luck.
For you understanding you can refer the following as well,
http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_IO_CurrentWorkingDirectory
http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_Reflection_WheretheClassloadedfrom
Good luck.
you can find out what is the absolute path of the working dir by:
String str = new File("").getAbsolutePath()