提高程序的可移植性 [英] Raising the portability of program

查看:106
本文介绍了提高程序的可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了我的项目。在jbutton项目下运行一个静态网页。但是,我的java代码rads我的文件夹路径C:Users / Emre / Documents / etc ...

当我将项目移动到另一台PC时,代码不起作用。我需要像C#.Net这样的Application.StartupPath + webpage.html。

我尝试过user.dir但是没有用。



 尝试 {

String developersSayfasi = System.getProperty( user.dir\\site\\site.html) ;

Runtime.getRuntime()。exec( cmd / c start + developersSayfasi);
}
catch (Exception ex){
JOptionPane.showMessageDialog(null, 错误,网页不存在);
}

解决方案

您可以使用文件(MyClass.class.getProtectionDomain()。getCodeSource( ).getLocation()。getPath());







回答后续问题:



我不知道什么是 GraduationProject 。看看这个表达式的结果。这是一条真正的道路吗?如果它几乎在您需要的位置,请相应地进行调整。在执行下一行之前,我建议在调试器下执行此操作,或者提供此字符串的测试日志。特别是,请确保您没有使用双目录分隔符。 (对不起,我不记得位置是否返回带有结束路径分隔符的目录...)



下一行是完全错误的!你永远不需要CMD。这仅仅是他们的另一个应用程序,与其他应用程序没什么不同。这是使用它的常见错误。不幸的是,我立即找到了一些使用CMD的建议。这是一个耻辱!忘了CMD.EXE。



你需要用来运行的字符串只是你的变量 run 。您的 getRuntime 是正确的:它通过Shell执行应用程序。 Shell使用有关应用程序类型的Registry数据,因此它知道该文件使用的应用程序。在您的情况下,默认浏览器(甚至已经启动)。



有关确认,请参阅此代码用于运行.mp3文件的默认播放器:

http://www.dreamincode.net/论坛/主题/ 184167-java-shell-execute-equivalent / [ ^ ]。



参见: http://www.java2s.com/Code/Java/Development-Class/Helpermethodtoexecuteshellcommand.htm [ ^ ]。



但首先,检查run的值。问题出在那里。它可以像文件未找到的情况一样简单;检查路径是否正确并找到文件。



哦,还有:你的\\很糟糕。它只适用于Windows,完全不可移植。我相信你可以使用/。但要真正便携,你需要使用:

System.getProperty(path.separator)。请参阅:

http://www.roseindia.net/answers/viewqa/Java-Beginners/10985-Platform-dependent-values-like-line-separator,-path-separator.html [< a href =http://www.roseindia.net/answers/viewqa/Java-Beginners/10985-Platform-dependent-values-like-line-separator,-path-separator.html\"target =_ blanktitle = 新窗口> ^ ]







由关于.NET:我声称 Application.StartupPath 不够好。我相信任何人都应对自己的主张负责,以下是我的详细解释和全面的建议:



可执行位置:如何找到我的程序目录 [ ^ ],

当前目录和特殊文件夹:如何查找我的程序目录 [ ^ ]。< br $> b $ b

在实践中,这一点非常重要,请学习和使用它。



-SA

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.

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");
} 

解决方案

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


这篇关于提高程序的可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆