Windows – 从java代码中编程查找绝对的java.exe路径

前端之家收集整理的这篇文章主要介绍了Windows – 从java代码中编程查找绝对的java.exe路径前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我有一个由用户启动的 java jar或类文件(假设 java路径设置在环境变量中),那么我如何从代码中找出java.exe / javaw.exe的绝对路径文件正在启动.

像ubuntu一样,我们可以运行:%哪个java和它显示的路径.

但是在Windows上,如果我检查System.getenv(),可能会出现多个路径,例如旧版本或新版本.如果通过cmd行,我运行java -version它不会显示路径.

你可以通过纯Java或Windows上的命令行告诉我如何可以找出javaw.exe的位置?

  1. String javaHome = System.getProperty("java.home");

Can you tell me either through pure Java … on windows how is it possible to find out the location of javaw.exe?

例如.

  1. import java.io.File;
  2.  
  3. class JavawLocation {
  4.  
  5. public static void main(String[] args) {
  6. String javaHome = System.getProperty("java.home");
  7. File f = new File(javaHome);
  8. f = new File(f,"bin");
  9. f = new File(f,"javaw.exe");
  10. System.out.println(f + " exists: " + f.exists());
  11. }
  12. }

产量

  1. C:\Program Files (x86)\Java\jdk1.6.0_29\jre\bin\javaw.exe exists: true
  2. Press any key to continue . . .

是的,我有信心在JRE工作.

猜你在找的Windows相关文章