文件存在时抛出 FileNotFoundException
我面临这个奇怪的问题。
我正在尝试读取位于另一台计算机中的文件作为共享资源:
\\remote-machine\dir\MyFileHere.txt
当我运行独立应用程序(一个 16 行 java 文件)时,一切都很好。 但是,当我尝试从服务器“引擎”(这是一个应用程序引擎,非常类似于可以运行 java 程序的 Java EE 应用程序服务器)使用相同的类和相同的方法读取相同的文件时,出现“FileNotFoundException”被抛出。
我想我会得到某种权限,所以我将资源映射为驱动器:K:\
重新运行我的java文件,读取,很好。
在“引擎”中重新运行我的java文件-> 文件未找到异常。
当我将文件复制到本地计算机( C:\MyFileHere.txt )时,不会引发异常。
问题
可能是什么原因导致此 FileNotFoundExcecption?
我正在使用 Java 1.5
据我所知,该引擎几乎透明地使用 java。
有人遇到过类似的事情吗?
附加问题? 解决这个问题的好方法是什么? 我开始考虑安装一个 tomcat 来提供这些文件并通过 http 读取它们,但我认为这太多了,这就是为什么 SMB 协议是第一位的,不是吗? 无论如何我可能都无法打开套接字。
安全管理器是否可能是原因(我以前从未使用过它,但我知道它存在)
如果是这种情况,不会抛出 SecurityException 吗?
多谢。
编辑
已解决。 谢谢Steve W。
原来这个引擎是用ZeroG的“LaunchAnywhere”启动的。 因此,创建了一个 .exe,它将使用指定的应用程序运行 JVM。
该应用程序本身就是启动器。 当它启动引擎时,不知何故(我不知道为什么或如何)拥有 JVM 进程的用户是 SYSTEM。 正如史蒂夫指出的那样,该用户没有网络访问权限,因此无法从共享资源或映射驱动器中读取数据。
解决方法(当我向制造商报告此情况时)是创建一个 .cmd 文件来手动启动引擎。 由于它将手动启动,因此用户确实可以访问网络。
我使用 SysInternals 中的“Process Explorer”来准确了解用于运行引擎应用程序的命令行。
真是一团糟!
感谢那些发布答案的人。
I'm facing this strange problem.
I'm trying to read a file that is located in another machine as a shared resource:
\\remote-machine\dir\MyFileHere.txt
When I run a standalone application ( a 16 lines java file ) everything is just fine. But when I attempt to read the same file with using the same class and the same method from a server "engine" ( this is an application engine, pretty much like a Java EE Application Server where you can run java programs ) the "FileNotFoundException" is thrown.
I though I would be some sort of permissions, so I map the resource as a drive: K:\
Re-run my java file, reads, fine.
Re-run my java file inside the "engine" -> FileNotFoundException.
When I copy the file into the local machine ( C:\MyFileHere.txt ) no exception is thrown.
Question
What may be causing this FileNotFoundExcecption?
I'm using Java 1.5
As far as I know the engine pretty much uses java transparently.
Anyone has faced something similar?
Additional question? What would be a good approach for workaround this? I'm starting to think about a tomcat installation serving those files and read them through http, but I think that's too much, that's why the SMB protocol is for in first place isn't it? And probably I won't be able to open sockets anyway.
Does security manager may be the cause ( I have never used that before, but I know it exists )
Wound't a SecurityException be thrown instead if that would be the case?
Thanks a lot.
EDIT
Solved. Thank you Steve W.
It turns out that this engine is launched with "LaunchAnywhere" from ZeroG. So, a .exe is created that in turn will run a JVM with the specified app.
This application it self is Launcher. When it start the engine, somehow ( I cannot figure out why or how ) the user that owns the JVM process is SYSTEM. AS Steve pointed out, this user doesn't have NETWORK access, and thus cannot read from a shared resource or a mapped drive.
The workaround ( while I report this to the manufacturer ) is to create a .cmd file to lauch manually the engine. Since it would be manually launched the user does have access to the network.
I've used "Process Explorer" from SysInternals to know exactly the command line used to run the engine app.
WHAT A MESS!
Thanks to those who posted answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
共享资源是否受用户名和密码保护? 如果是这样,您的应用程序引擎是否以该用户身份运行? 如果您的应用程序引擎作为 Windows 服务运行,则 Windows 服务无法作为“本地系统帐户”运行。 该帐户无法访问网络。 您必须将服务配置为以有权访问共享驱动器的用户身份运行。
Is the shared resource protected by a username and password? And if so, is your application engine running as that user? If your application engine is running as a Windows Service, the Windows service cannot be running as the "Local System Account". This account cannot access the network. You must configure your service to run as a user that has the rights to access the shared drive.
仔细检查该文件是否确实名为“MyFileHere.txt”而不是“MyFileHere.txt.txt”
如果您隐藏文件的扩展名,这是一个很容易错过的错误
Double check that the file is REALLY Called "MyFileHere.txt" and not "MyFileHere.txt.txt"
If you are hiding the extention of the file this is an easy mistake to miss
您是否检查过服务器上的事件日志以查看是否被拒绝?
该程序可能在与您想象的不同的用户帐户下运行。
我不熟悉 Java,但我知道我编写的一些程序必须允许网络服务访问资源。
事实上,我看到您现在已经勾选了一个答案作为正确的答案。
哦,这和我的答案一样:) 酷!
Have you checked the event logs on the server to see if it is being rejected?
it could be that the program is running under a different user account than you think.
I'm not familiar with Java, but i know with some programs i've written i've had to allow Network Service to access the resources.
Actually i see that you have now ticked an answer as the correct one.
oh and it was the same as my answer :) Cool!
我曾经遇到过类似的问题。 我认为这与 java 解析远程文件 URI 的方式有关。 尝试以下操作并查看它是否有效:
File:////remote-machine/dir/MyFileHere.txt
我使用以下示例来验证我的盒子中的共享文件夹中是否存在文件并工作:
I had a similar problem once. I think it has to do with the way java resolves remote files URI's. Try the following and see if it works:
File:////remote-machine/dir/MyFileHere.txt
I used the folowing example to verify the existence of a file in shared folders in my box and worked: