Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I finished my project. Under a jbutton project runs a static web page. However, my java code rads my folder path C:Users/Emre/Documents/etc...
When I moved the project to another pc, the code does not work. I need something as Application.StartupPath + webpage.html like in C#.Net.
I tried user.dir but does not work.

Java
try{ 
   
String developersSayfasi=System.getProperty("user.dir\\site\\site.html");

Runtime.getRuntime().exec("cmd /c start "+developersSayfasi);
} 
catch(Exception ex){ 
JOptionPane.showMessageDialog(null,"Error, the web page does not exists");
} 
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-13 20:05pm    
It is not called "raising the portability", it's called "making a complete failure to work"... :-)
Even Application.StartupPath is not a good way... But of course you are right now, you really need a location of your class of you entry point.
—SA
FoxRoot 18-Jan-13 20:30pm    
I am just beginner :) You are so helpful.
I completed my graduation project with your and TorsentH's answer :) Thanks a lot.
Sergey Alexandrovich Kryukov 18-Jan-13 21:21pm    
Congratulation with your graduation! It's a pleasure to do something good for people. And it's good that you are able to understand humor and irony, unlike many others. This is a key to progress. :-)
—SA

1 solution

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
 
Share this answer
 
v4
Comments
FoxRoot 18-Jan-13 20:10pm    
Thanks for your consideration Sergey. How can I use this? I posted my code, wher should I locate this and how ? I am sorry, I am asking too much.
Sergey Alexandrovich Kryukov 18-Jan-13 21:01pm    
Not too much :-). You need to use it at the point you need a path, apparently. The idea is: you need to have the class which is located at required relative location. When the application is deployed and executed, it will be the location relative to the root. In you case, you need to be at the location of the root directory of your application ("entry point").
—SA
FoxRoot 18-Jan-13 20:39pm    
Sergey, I updated my code as follows. However, stil does not work.

String run= GraduationProject.class.getProtectionDomain().getCodeSource().getLocation().getPath()+"\\site\\site.html";

Runtime.getRuntime().exec("cmd /c start "+run);
Sergey Alexandrovich Kryukov 18-Jan-13 21:18pm    
A number of issues are possible with this code; and I already can see a couple of them. For further advice, please see my updated answer, after [EDIT].
When (and it) you see it's reasonable, please accept my answer formally (green button) — thanks.
—SA
Sergey Alexandrovich Kryukov 18-Jan-13 21:29pm    
And also [EDIT #2], by the way...
Enjoy!
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900