使用“class.getResource(”/“)”时出现NullPointerException(NullPointerException when using “class.getResource(”/“)”)
我正在编写一个包装在两个Jar文件中的应用程序:startup.jar和resource.jar。 在resource.jar中我有一个java文件(ResourceFile.java)具有以下代码:
String dir = ResourceFile.class.getResource("/").getPath();
应用程序应该通过运行“startup.jar”(两个jar文件放在同一个目录中)来启动,但我总是在上面的代码中得到NullPointerException,我也试图将它改为下面的代码:
String dir = ResourceFile.class.getClassLoader().getResource("").getPath();
得到了同样的错误。
System.out.println(ResourceFile.class.getResource("/") == null);
打印真实。
我环顾四周,找不到可以帮助我解决问题的解决方案。
I'm writing an application which is packaged in two Jar files: startup.jar and resource.jar. In resource.jar I have a java file(ResourceFile.java) has below code:
String dir = ResourceFile.class.getResource("/").getPath();
The application should be started up by running the "startup.jar"(two jar files are put in the same directory), but I always get NullPointerException at above code, I have also tried to change it to below code:
String dir = ResourceFile.class.getClassLoader().getResource("").getPath();
Got the same error.
System.out.println(ResourceFile.class.getResource("/") == null);
print true.
I have looked around for a bit and I couldn't find a solution that could help me solve the problem.
原文:https://stackoverflow.com/questions/41789127
最满意答案
在Mac上,默认情况下,mysql安装在
/usr/local/mysql/
因此,二进制文件的路径(mysql,mysqladmin等)应为
/usr/local/mysql/bin
On a Mac, by default, mysql is installed to
/usr/local/mysql/
The path to the binaries (mysql, mysqladmin, etc.) should therefore be
/usr/local/mysql/bin
相关问答
更多-
在Mac上,默认情况下,mysql安装在/usr/local/mysql/ 因此,二进制文件的路径(mysql,mysqladmin等)应为/usr/local/mysql/bin On a Mac, by default, mysql is installed to /usr/local/mysql/ The path to the binaries (mysql, mysqladmin, etc.) should therefore be /usr/local/mysql/bin
-
在我的Mac OSX 10.6.8上使用Rails应用程序并遗漏了一些软件(Working on a Rails app and missing some software on my Mac OSX 10.6.8)[2023-11-24]
只需将它添加到您的Gemfile并进行bundle install source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.1' # Use jdbcsqlite3 as the database for Active Record gem 'activerecord-jdbcsqlite3-adapter' # Use SCSS for ... -
以下是在Mac OSX机器上正确安装和测试YAWS(另一个Web服务器)的步骤: 安装: 访问http://yaws.hyber.org/download/ 获取最新来源 - 例如'yaws-X.XX.tar.gz'。 解压缩它(对于本教程,我们假设/ Users / Name / Desktop / yaws /)。 打开终端(可在Applications / Utilities中找到)。 键入:'cd /Users/Name/Desktop/yaws/yaws-X.XX/'(不含引号)。 键入:'./c ...
-
如何在Mac OSX 10.6上安装Perl DateTime模块(How do I install the Perl DateTime module on Mac OSX 10.6)[2023-08-27]
Xcode在Lion上面临着一个已知的问题 ,Apple无法帮助与nm搞混,再次打破Perl( 以前 ),谁知道还有什么 p5p讨论 perlbrew的信息 你使用的是不受支持的Perl。 升级到稳定。 known problem with Xcode on Lion, Apple couldn't help messing around with nm, breaking Perl again (previously) and who knows what else p5p discussion perl ... -
在OS X上,可以使用pthread.h中定义的pthread_threadid_np。 注意_np后缀表明它是POSIX的扩展。 On OS X pthread_threadid_np, defined in pthread.h, can be used. Note the _np suffix suggests that it is an extension to POSIX.
-
我可以在OSX 10.6.8(Snow Leopard)上安装Xcode 4.2吗?(Can I install Xcode 4.2 on OSX 10.6.8 (Snow Leopard)?)[2023-03-22]
不,不可能,您需要最新版本的Mac OSX:Snow Lion(10.8)才能运行xCode 4.2。 幸运的是,升级并不是那么昂贵,并且据说是物有所值(已经购买了升级,但没有自己安装:-)) No, not possible, you need the latest version of Mac OSX: Snow Lion (10.8) to run xCode 4.2. Luckily, the upgrade isn't that expensive, and is said to be worth ... -
您的开发人员工具未正确安装。 我建议安装Xcode 4。 Your developer tools are not installed correctly. I recommend installing Xcode 4.
-
我有一个肮脏的把戏。 使用cpan,我仍然无法安装它。 但是我下载了PadWalker-1.98.tar.gz 打开Makefile.PL并删除MIN_PERL_VERSION =>“5.008001”行, 我做了$ perl Makefile.PL 参考https://rt.cpan.org/Public/Bug/Display.html?id=69506 我打开生成的Makefile并删除了Makefile中'-arch ppc'的所有实例。 最后$ make&$ make install 现在我能够在 ...
-
如何在osx 10.6.8上安装python库Pyexiv2和Gexiv2?(How to install python library Pyexiv2 and Gexiv2 on osx 10.6.8?)[2024-03-18]
使用Homebrew包管理器非常简单: brew install exiv2 pyexiv2 如果您使用的是Macports,它还有exiv2公式: http://www.macports.org/ports.php?by=name&substr=exiv - 编辑 - 旁注:我在“import pyexiv2”中遇到了段错误。 原来我的系统Python 2.7.2和自制的Python 2.73同时安装。 我卸载了酿造的Python之后问题就消失了。 相关信息可在此处找到: https://answers ... -
移动评论回答。 1.)回答Please install the mysql adapter: gem install activerecord-mysql-adapte (mysql is not part of the bundle. Add it to Gemfile.) ANS:在你的database.yml文件中,适配器应该是mysql2而不是mysql 2.) rake aborted! Can't connect to local MySQL server through socket /tmp ...