this.getClass()。getResource(“”)。getPath()返回错误的路径 [英] this.getClass().getResource("").getPath() returns an incorrect path

查看:233
本文介绍了this.getClass()。getResource(“”)。getPath()返回错误的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的计算机科学决赛编写一个小的简单Java程序,该程序需要获取当前正在运行的类的路径。类文件位于C:\2013\game\文件夹中。

I am currently making a small simple Java program for my Computer Science Final, which needs to get the path of the current running class. The class files are in the C:\2013\game\ folder.

要获取此路径,请在主类构造函数中将此代码段称为: / p>

To get this path, I call this code segment in my main class constructor:

public game(){
    String testPath = this.getClass().getResource("").getPath();
    //Rest of game
}

但是,此命令返回此命令字符串: /尽管正确的输出是 C:/ 2013 / game
另外,我尝试通过使用以下代码来纠正此问题:

However, this command instead returns this String: "/" despite the correct output being "C:/2013/game" Additionally, I attempted to rectify this by using this code:

public game(){
    String testPath = this.getClass().getClassLoader().getResource("").getPath();
}

这将返回NullPointerException,该异常源于getClassLoader()返回null的事实,尽管我在Eclipse IDE上工作。有想法吗?

This returns a NullPointerException, which originates from the fact that getClassLoader() returns null, despite working on my Eclipse IDE. Any Ideas?

推荐答案

如果您要在与代码相同的路径中加载文件,则建议您将其放入

If you want to load a file in the same path as the code then I suggest you put it in the same root folder as the code and not the same path as the class.

原因:类可以放在jar中,数据文件可以放在同一jar中,但它的更多信息

Reason : class can be inside a jar, data file can be put in same jar but its more difficult to edit and update then.

还建议您查看注释中建议的首选项类: http://www.javacodegeeks.com/2011/09/use-javautilprefspreferences-instead-of.html ,尽管在​​某些情况下,我认为可以拥有自己的数据/ excel / csv / java.util.Properties文件

Also suggest you see the preferences class suggested in comments : http://www.javacodegeeks.com/2011/09/use-javautilprefspreferences-instead-of.html though in some cases I think its okay to have your own data/ excel/csv/ java.util.Properties file

不知道为什么它可以在日食中工作,但我建议您集中精力从命令提示符/终端运行它,因为它是上线时的真实模式

Not sure about why it is working in eclipse but I would suggest you focus on running it from a command prompt/ terminal as that is the 'real mode' when it goes live

您可以只要求您的课程

    String s = getClass().getName();
    int i = s.lastIndexOf(".");
    if(i > -1) s = s.substring(i + 1);
    s = s + ".class";
    System.out.println("name " +s);
    Object testPath = this.getClass().getResource(s);
    System.out.println(testPath);

这将为您提供


名称TstPath.class
文件:/java/Projects/tests3b/build/classes/s/TstPath.class

name TstPath.class file:/java/Projects/tests3b/build/classes/s/TstPath.class

这是我的月食构建路径...

Which is my eclipse build path ...

需要对此进行解析,以获取加载类的路径。

need to parse this to get the path where the class was loaded.

记住:


  1. 应用程序可以从其他地方启动

  2. 类可以放在jar中,则路径将不同(将指向其中的一个jar和文件

  3. 类路径在运行时可以有很多,并指向1

  4. a类可能是在运行时通过网络/代理/注入等创建的,因此没有文件源,因此这不是通用的解决方案。

  5. 思考一下您想在更高层次上实现的目标并发布该问题,这意味着您为什么要使用此路径?

  6. 您是否想要应用程序路径:-

  1. App could be started from elsewhere
  2. class can be in jar then path will be different (will point to a jar and file inside that
  3. classpaths can be many at runtime and point 1
  4. a class might be made at runtime via network/ Proxy / injection etc and thus not have a file source, so this is not a generic solution.
  5. think what you want to acheive at a higher level and post that question. meaning why do you want this path?
  6. do you want the app path :-

File f = new File( ./);

f.getCanonicalPath(); // ...

File f = new File("./");
f.getCanonicalPath();//...

因此可以从文件夹c:\app1\run\启动应用程序

So an app can be started from folder c:\app1\run\

罐子可能是在c:\app1\libsMain\myapp.jar

The jar could be at c:\app1\libsMain\myapp.jar

和一个辅助jar可以在c:\commonlibs\set1

and a helper jar could be at c:\commonlibs\set1

因此,这只会告诉您JVM在哪里找到您的类,这可能是您需要的,也可能不是。

So this will only tell you where the JVM found your class, that may or maynot be what you need.

如果在jar中将在Unix或Windows中为您提供类似的信息

if inside a jar will give you some thing like this in unix or windows


jar:file:c:\app\my.jar!/ s /TstPath.class

jar:file:c:\app\my.jar!/s/TstPath.class

如果package是s并且class是TstPath,则可以确保它可以正常工作,因为类必须存在...

If package is s and class is TstPath, you can be sure this will work as the class has to be there ...

现在要解析它,您可以查找您的类名并删除/或\,直到获得所需的路径为止。字符串lastIndexOf将帮助

now to parse this you can look for your class name and remove / or \ till you get path you want. String lastIndexOf will help

这篇关于this.getClass()。getResource(“”)。getPath()返回错误的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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