PROBLEM: Hello everyone! I have created a Maven project in which i am building
a RESTful API, now i have created few .json files in which i want
to store my JSON data in src/main/resources , but as i can see
every time i make a change ( example: register a user - write in a
file ) all changes get lost whenever i redeploy the WAR file or the
server gets restarted/reloaded.
Can someone please tell me how to use system PATH variable to fix
my problem? I want to store files somewhere i will be able to update
them without having to worry if server is being reloaded or not. Thank you in advance!
SMART WAY, with java configuration and classpath (unix example, launch your application this way)
java -classpath $PATH <your-class-name>
or
setenv CLASSPATH = $PATH
see unix and windows instructions.
BAD WAY, within application code
You can read the PATH variable from the OS environment calling System.getenv() and then looking for the "PATH" elements within the returned map.
Then you have to parse the string (e.g. with StringTokenizer) obtained to get each folder within the PATH variable
you have to look for your JSON files in each of the folder from previous point
Related
Would you please help how to set the variable used in my java program, without much worry as current I have to run my code locally on the windows machine.
so the entire files which I am reading are at windows d:// directory, consider having more file to read and write from different locations from windows D drive.
according to my business logic.
same directory structure I am using on my CentOS server side. but the problem is in CentOS directory starts with /home/dir1/ and so one, so who to make this thing work.
As on each time I build my jar for deploy into server side. I have to change those variable names according to with Windows or CentOS.
Do you have any other easy ways, because of many time while testing I forget to make those directory change before testing on windows and deploying on the server side.
Really much appreciated in advance for any help or any link which can guide me to achieve that.
Instead of hard-coding the path in your class, externalize the configuration. There are many options for that.
Pass the path using System properties:
java -Dfile.location=/home/dir1/ -jar jarfile.jar
In the program then, you can use that property:
File directory = new File(System.getProperty("file.location");
If you run a main method in a class in the jar, pass program arguments:
java -jar jarfile.jar "D:/directory"
This can then be read in your main program
public static void main(String[] args) {
File directory = new File(System.getProperty(args[0]);
//... rest of the program
There are many other options, including property files, environment variables, JNDI, all depending on your runtime environment.
I am completely new to programming and I am now doing a project that I need to read a specific XML file in java. My code is using the absolute path of the file I need but I have to distribute the project to somewhere. That required XML file will not be in the project folder but in another folder which I will distribute with the JAR file of the project. Is it possible for it to get that path if I don't know where the user will put my program and the folder containing the XML file?
The program is supposed to run on Windows.
You have different possible solutions:
pass the file location as parameter to the main method (if you have a main method)
set an system property with the location of the file (you can do that with the -D option on the java command, with the syntax -Dproperty=value)
set an environment variable (with export command in linux)
put the file in the classpath and retrieve it with getResourceAsStream
access the file from an external server exposing it via HTTP
When I run the following command:
bin/tdbloader2 --loc=/store/data/here /seed/data/serverfault-dump.nt
And the response I get is:
10:52:31 -- TDB Bulk Loader Start
10:52:31 Data phase
Error: Could not find or load main class com.hp.hpl.jena.tdb.store.bulkloader2.CmdNodeTableBuilder
Which is most likely caused by a problem with my environment variables, somewhere, of something. The problem is, I don't often work with Java and so I don't know enough to know how to figure out what that class cannot be found.
How do make tdbloader2 find the appropriate class?
I had forgotten to set the JENAROOT path variable.
https://jena.apache.org/documentation/tools/
An environment variable JENAROOT is used by all the command line tools to
configure the class path automatically for you. You can set this up as follows:
On Linux / Mac
export JENAROOT=the directory you downloaded Jena to export
PATH=$PATH:$JENAROOT/bin On Windows
SET JENAROOT=the directory you downloaded Jena to SET
PATH=%PATH%;%JENAROOT%\bat
At a minimum you need to set the TDBROOT environment variable to the directory containing your TDB download. This will be the directory above bin as some of the scripts use ${TDBROOT}/bin/foo to launch other scripts
The script attempts to automatically construct a valid class path by calling the tdb_path script which calls either make_classpath_mvn or make_classpath depending on your environment. If you run the tdb_path script directly you can see if it generates a sane looking class path or not (or add what it does generate to your question if the output doesn't provide anything helpful).
I have 2 property files which are part of my java eclipse project. Now i need to pass the path of these 2 files as a system property like -Dpath1="pathfile1" as project will only accept it as a system property.
One option is that i can specify the absolute path but i want to specify a path relative to my project something like -Dpath1=$Project_Dir/resource/file1. Reason being, if project directory is moved then project will start failing.
I am using eclipse to run this project and dont want to declare any new system variable for supporting relative path, so for example dont want to create a new system variable Project_Dir . Is it possible to achieve this and how?
You can set system properties with:
System.setProperty("path1", yourPathHere);
Now, you only have to build the correct path, relative to where your project is. To do this, start off with the location of the project: How to get the path of a running JAR file?
When using setProperty, you can reference internal project files if you use a plain relative path.
E.g. to reference a file located at /abc/def/workspace/project/lib/driver.exe:
System.setProperty("driver", "lib/driver.exe");
It's deceivingly simple.
Yes, you can do it.Eclipse have build-in Path variable PROJECT_LOC, it stores location of project folder, e.x. c:\Workspace\Project1.
You can use this property in launch configuration.
I am developing a small Java application using Swing, that is supposed to run on Windows/Linux and MacOS.
Eventually it will ship as a runnable jar with some config files in the same folder. To load them I need the path to the folder the jar is stored in within the program.
There are already a couple of threads like this one or this one.
My problem is, that all the solutions discussed there work fine, when I run the program from within eclipse or call the runnable jar from a terminal like so:
java -jar /path/to/jar/jarfile.jar
However when I click on the jar file in Cinnamon or Gnome (which is what most of the users will know to do), I do not get the correct paths. (MacOS users report the same issue)
Here is what I've tried so far and what the output is when run via double click (all those display the desired path when run from eclipse or a terminal):
ClassLoader.getSystemClassLoader().getResource(".").getPath()
Output: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Output: ./
System.getProperty("user.dir");
Output: /home/myusername
Is there any other way to do it or am I doing something wrong when exporting the jar? Any help would be appreciated!
Cheers
Nick
Make it simple, and use a startup script (.bat/.sh file) to run your application. This startup script will get the path of its own location in the filesystem, and pass it as an argument or system property to the Java application. This has the additional advantage of being able to pass other arguments, like the size of the heap, etc.
On windows, %~dp0 is the path of the directory containing the executed bat file. On Unix, you can use $(dirname $0).
You could store all config files as resources in a jar, and copy them to files in home.dir + ".AppName/".
Painful as it is, the Preferences API, Preferences.systemNodeForPackage, seems the wisest alternative, if there is little structured config data. There is an inputStream method for import; your initial config template could be a resource in the jar.
Just get the class path using System.getProperty("java.class.path") and scan it for your ".jar" name. Note that path separators are OS dependent (File.pathSeparator)