<i id='DaU0h'><tr id='DaU0h'><dt id='DaU0h'><q id='DaU0h'><span id='DaU0h'><b id='DaU0h'><form id='DaU0h'><ins id='DaU0h'></ins><ul id='DaU0h'></ul><sub id='DaU0h'></sub></form><legend id='DaU0h'></legend><bdo id='DaU0h'><pre id='DaU0h'><center id='DaU0h'></center></pre></bdo></b><th id='DaU0h'></th></span></q></dt></tr></i><div id='DaU0h'><tfoot id='DaU0h'></tfoot><dl id='DaU0h'><fieldset id='DaU0h'></fieldset></dl></div>
<tfoot id='DaU0h'></tfoot>
  • <legend id='DaU0h'><style id='DaU0h'><dir id='DaU0h'><q id='DaU0h'></q></dir></style></legend>

      1. <small id='DaU0h'></small><noframes id='DaU0h'>

          <bdo id='DaU0h'></bdo><ul id='DaU0h'></ul>

        如何在Java中获取当前类名,包括包名?

        时间:2023-09-27

        <small id='CYExG'></small><noframes id='CYExG'>

            <tbody id='CYExG'></tbody>

          <tfoot id='CYExG'></tfoot>
          1. <i id='CYExG'><tr id='CYExG'><dt id='CYExG'><q id='CYExG'><span id='CYExG'><b id='CYExG'><form id='CYExG'><ins id='CYExG'></ins><ul id='CYExG'></ul><sub id='CYExG'></sub></form><legend id='CYExG'></legend><bdo id='CYExG'><pre id='CYExG'><center id='CYExG'></center></pre></bdo></b><th id='CYExG'></th></span></q></dt></tr></i><div id='CYExG'><tfoot id='CYExG'></tfoot><dl id='CYExG'><fieldset id='CYExG'></fieldset></dl></div>

                <bdo id='CYExG'></bdo><ul id='CYExG'></ul>

                  <legend id='CYExG'><style id='CYExG'><dir id='CYExG'><q id='CYExG'></q></dir></style></legend>
                  本文介绍了如何在Java中获取当前类名,包括包名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在做一个项目,一个要求是如果 main 方法的第二个参数以/"(对于 linux)开头,它应该将其视为绝对路径(不是问题),但如果它不以/"开头,它应该获取类的当前工作路径,并将给定的参数附加到它上面.

                  I'm working on a project and one requirement is if the 2nd argument for the main method starts with "/" (for linux) it should consider it as an absolute path (not a problem), but if it doesn't start with "/", it should get the current working path of the class and append to it the given argument.

                  我可以通过以下几种方式获取类名:System.getProperty("java.class.path")new File(".")>getCanonicalPath() 等等……

                  I can get the class name in several ways: System.getProperty("java.class.path"), new File(".") and getCanonicalPath(), and so on...

                  问题是,这只给了我存储包的目录 - 即,如果我有一个存储在.../project/this/is/package/name"中的类,它只会给我 "/project/" 并忽略实际 .class 文件 所在的包名.

                  The problem is, this only gives me the directory in which the packages are stored - i.e. if I have a class stored in ".../project/this/is/package/name", it would only give me "/project/" and ignores the package name where the actual .class files lives.

                  有什么建议吗?

                  这是从练习描述中摘录的解释

                  Here's the explanation, taken from the exercise description

                  sourcedir 可以是绝对的(以/"开头)或相对于我们运行程序的位置

                  sourcedir can be either absolute (starting with "/") or relative to where we run the program from

                  sourcedir 是 main 方法的给定参数.我怎样才能找到那条路?

                  sourcedir is a given argument for the main method. how can I find that path?

                  推荐答案

                  使用 this.getClass().getCanonicalName() 获取完整的类名.

                  Use this.getClass().getCanonicalName() to get the full class name.

                  请注意,包/类名 ("abC") 与 .class 文件 (a/b/C.class) 的路径不同,使用包名/类名派生路径通常是不好的做法.类文件/包的集合可以在多个不同的类路径中,可以是目录,也可以是jar文件.

                  Note that a package / class name ("a.b.C") is different from the path of the .class files (a/b/C.class), and that using the package name / class name to derive a path is typically bad practice. Sets of class files / packages can be in multiple different class paths, which can be directories or jar files.

                  这篇关于如何在Java中获取当前类名,包括包名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么我的子类不能访问其超类的受保护变量, 下一篇:如何在我的 Android 应用中使用和打包 JAR 文件?

                  相关文章

                  最新文章

                  <tfoot id='H217K'></tfoot>

                    <small id='H217K'></small><noframes id='H217K'>

                  1. <legend id='H217K'><style id='H217K'><dir id='H217K'><q id='H217K'></q></dir></style></legend>

                      <i id='H217K'><tr id='H217K'><dt id='H217K'><q id='H217K'><span id='H217K'><b id='H217K'><form id='H217K'><ins id='H217K'></ins><ul id='H217K'></ul><sub id='H217K'></sub></form><legend id='H217K'></legend><bdo id='H217K'><pre id='H217K'><center id='H217K'></center></pre></bdo></b><th id='H217K'></th></span></q></dt></tr></i><div id='H217K'><tfoot id='H217K'></tfoot><dl id='H217K'><fieldset id='H217K'></fieldset></dl></div>
                        <bdo id='H217K'></bdo><ul id='H217K'></ul>