You can use
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
.
[EDIT #1]
Answering the follow-up questions:
I have no idea what is "
GraduationProject
". Look at the result of this expression. Is it a real path? If it's almost where you need, adjust it accordingly. I would advice to do it under the debugger, or provide a test log of this string, before you execute next line. In particular, make sure you are not using double directory separator. (Sorry, I don't remember if location returns directory with ending path separator or not…)
And the next line is totally wrong! You never need "CMD". This is nothing more them yet another application, no different than any other. This is a common mistake to use it. Unfortunately, I immediately found some "advice" to use "CMD". It's a shame! Just forget about CMD.EXE.
The string you need to use to run is just your variable
run
. Your
getRuntime
is correct: it executes application via the Shell. And the Shell uses Registry data about application type, so it "knows" what application to use with this file. In your case, a default browser (have it been even started).
For some confirmation, please see this code used to run a default player for a .mp3 file:
http://www.dreamincode.net/forums/topic/184167-java-shell-execute-equivalent/[
^].
See also:
http://www.java2s.com/Code/Java/Development-Class/Helpermethodtoexecuteshellcommand.htm[
^].
But first of all, check up the value of "run". The problem is there. It could be as simple as file not found situation; check that the path is correct and the file is found.
Oh, also: your "\\" is bad. It's only for Windows, totally unportable. I believe you can use "/". But to be really portable, you need to use:
System.getProperty("path.separator"). Please see:
http://www.roseindia.net/answers/viewqa/Java-Beginners/10985-Platform-dependent-values-like-line-separator,-path-separator.html[
^]
[EDIT #2]
By the way, about .NET: I claimed that
Application.StartupPath
is not good enough. As I believe anyone should be responsible for own claims, here are my detail explanations and comprehensive advice:
executable location:
How to find my programs directory[
^],
current directory and "special folders":
How to find my programs directory[
^].
In practice, this is really important to know, please learn and use it.
—SA