This question already has answers here:
Changing the current working directory in Java?
(14 answers)
Closed 9 years ago.
I'm writing some unit tests. I'm running the tests by invoking the classes directly (rather than invoking another program). The problem is that some of these classes use data defined by relative paths, so they require that the program is started in a specific directory. How can I change this in Java?
For instance, my unit test starts in C:/unittest, and the data I need is in C:/OtherProject. I don't want to modify the code of the other project if possible, is there something like this in java:
File.setWorkingDir("C:/OtherProject");
That way when something like
File file = new File("data/data.csv");
Will read C:/OtherProject/data/data.csv instead of C:/unittest/data/data.csv.
Updating my answer, since VolkerSeibt pointed out that it was incorrect. Good catch.
This is possible through System.setProperty. You can change the current working directory by changing the "user.dir" system property:
System.setProperty("user.dir", "/foo/bar");
See http://www.javacodex.com/Files/Set-The-Current-Working-Directory for further explanation.
Related
This question already has answers here:
Totally Confused with java.exe
(3 answers)
How is JNI_CreateJavaVM invoked when running a java app from the command line
(1 answer)
Closed 1 year ago.
My question is regarding the java executable, the one that you use to run a Java program and that on Linux it is found for example in /usr/bin/java.
I have been experimenting a bit as I want to look into how everything happens behind the scenes, like how does the bytecode gets loaded, how is the execution of the actual Java program starting and other details that may not be so straightforward.
Until now I have looked at the execution with strace and found that a new thread is created and that thread is the one on which the Java program actually gets executed (from another question that I posted). From what I understand the java executable is a launcher of some sort, but I do not understand all the operations that happen behind the scenes.
So, what exactly is the java executable and is there any place where I can find the source code for it (this would really help me)?
The primary source file for the launcher in the current development JDK can be found here: https://github.com/openjdk/jdk/blob/master/src/java.base/share/native/libjli/java.c
As you see, it's quite short and delegates most of the work to other pieces of code, but should be useful as a starting point.
If you want to see the source for other JDK versions (this is basically the main development repo for future Java versions), you need to look into the appropriate repository.
This question already has answers here:
How to get a list of current open windows/process with Java?
(14 answers)
Closed 6 years ago.
I am currently writing a Java Application, that needs to filter the name of the Program whichs UI is front. Sorry for my bad english.
So let's say the Java App is running in Background and I open Windows -> Games -> Minesweeper then i want the App to only tell me "Active: Minesweeper". Without any additional information. Just the name "Minesweeper"
I already tried using JavaNativeAccess but I'm still unfamiliar with it.
Thanks you all in advance
You might want to look at the following link which might be of help:
How to get a list of current open windows/process with Java?
This question already has answers here:
How to get the current working directory in Java?
(26 answers)
Closed 7 years ago.
I know the title is a bit vague, so let me explain my explanation in more detail:
I have a Java application
I want to access the directory of my application on the clients computer, to store text-files.
My problem:
How can I find the file/directory of my application on the clients computer in the first place? On different platforms, file directories are different, and the client can put the application on which folder they want.
Is there an easy way to do this?
Thanks
I think you are looking for the User's current working directory https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties()
String dir = System.getProperty("user.dir");
This question already has answers here:
Writing Eclipse plugin to modify Editor Preferences
(3 answers)
Closed 8 years ago.
I am working on a Eclipse plugin and I want to change some settings for the user in able to be able to use the plugin correctly
I need to make some changes in the settings of Eclipse using the code instead
For example:
If I want to access Window-Open perspective-Debug
Instead of telling the user these steps, I want to make it in the code
Any help ?
You have to be more specific, otherwise you're only going to end up at http://www.eclipse.org/eclipse/platform-core/documents/user_settings/faq.html . Your example is a bad one since you shouldn't be changing the perspective without the user's consent. Even then, that's not a Preference, that's an API call: http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/IWorkbenchWindow.html#openPage(java.lang.String, org.eclipse.core.runtime.IAdaptable) .
Your plug-in should not require the user to be in any specific perspective. They should be free to arrange the editor and views any way they like.
This question already has answers here:
Directory listener in Java
(3 answers)
Closed 9 years ago.
There are two directories with the same content (local directory and remote directory). sync files do when a change in one of the directories. I check directories continuously via Java and to get information for changes on the directories. But I'm looking for an alternative way.
My question is;
Does linux give informations for changes on a directory?
and if linux gives the information,
How to get the information from Linux continuously via Java, if there is a change in a directory?
Any idea?
Since you're using Java 6 (and therefore cannot use nio to watch a directory), you should take a look at JNotify. It lets you watch directories for file changes. It works in Linux by providing a native library which uses inotify.