简体   繁体   English

文件存在时抛出FileNotFoundException

[英]FileNotFoundException thrown when the file does exists

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. 当我运行一个独立的应用程序(16行java文件)时,一切都很好。 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. 但是,当我尝试使用相同的类和相同的方法从服务器“引擎”(这是一个应用程序引擎,非常类似于Java EE Application Server,您可以运行Java程序)读取相同的文件时,“FileNotFoundException”被扔了。

I though I would be some sort of permissions, so I map the resource as a drive: K:\\ 我虽然我会获得某种权限,所以我将资源映射为驱动器:K:\\

Re-run my java file, reads, fine. 重新运行我的java文件,读取,罚款。

Re-run my java file inside the "engine" -> FileNotFoundException. 在“引擎” - > FileNotFoundException中重新运行我的java文件。

When I copy the file into the local machine ( C:\\MyFileHere.txt ) no exception is thrown. 当我将文件复制到本地计算机(C:\\ MyFileHere.txt)时,不会抛出任何异常。

Question

What may be causing this FileNotFoundExcecption? 可能导致此FileNotFoundExcecption的原因是什么?

I'm using Java 1.5 我正在使用Java 1.5

As far as I know the engine pretty much uses java transparently. 据我所知,引擎几乎透明地使用java。

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? 我开始考虑提供服务这些文件的tomcat安装并通过http读取它们,但我认为这太多了,这就是为什么SMB协议首先用于它的原因不是吗? 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? 如果出现这种情况,是否会抛出SecurityException?

Thanks a lot. 非常感谢。

EDIT 编辑

Solved. 解决了。 Thank you Steve W. 谢谢史蒂夫W.

It turns out that this engine is launched with "LaunchAnywhere" from ZeroG. 事实证明,该引擎是使用ZeroG的“LaunchAnywhere”启动的。 So, a .exe is created that in turn will run a JVM with the specified app. 因此,创建一个.exe,然后将使用指定的应用程序运行JVM。

This application it self is Launcher. 它自己的这个应用程序是Launcher。 When it start the engine, somehow ( I cannot figure out why or how ) the user that owns the JVM process is SYSTEM. 当它启动引擎时,不知何故(我无法弄清楚为什么或如何)拥有JVM进程的用户是SYSTEM。 AS Steve pointed out, this user doesn't have NETWORK access, and thus cannot read from a shared resource or a mapped drive. 史蒂夫指出,该用户没有NETWORK访问权限,因此无法从共享资源或映射驱动器读取。

The workaround ( while I report this to the manufacturer ) is to create a .cmd file to lauch manually the engine. 解决方法(当我向制造商报告时)是创建一个.cmd文件来手动启动引擎。 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. 我使用了SysInternals的“Process Explorer”来确切地知道用于运行引擎应用程序的命令行。

WHAT A MESS! 真是一团糟!

Thanks to those who posted answers. 感谢那些发布答案的人。

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". 如果您的应用程序引擎作为Windows服务运行,则Windows服务无法作为“本地系统帐户”运行。 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”如果您正在隐藏文件的扩展名,这很容易错过

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. 我不熟悉Java,但我知道我编写的一些程序我必须允许网络服务访问资源。

Actually i see that you have now ticked an answer as the correct one. 实际上,我发现你现在已经选择了正确的答案。 oh and it was the same as my answer :) Cool! 哦,这和我的答案一样:)很酷!

I had a similar problem once. 我有一次类似的问题。 I think it has to do with the way java resolves remote files URI's. 我认为这与java解析远程文件URI的方式有关。 Try the following and see if it works: 尝试以下操作,看看它是否有效:

File:////remote-machine/dir/MyFileHere.txt 文件:////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: 我使用下面的示例来验证我的框中的共享文件夹中是否存在文件并且工作正常:

public static void main(String[] args) throws URISyntaxException{
    URI uri = new URI(args[0]); //args[0] = File:////remote-machine/dir/MyFileHere.txt
    File f = new File(uri);
    System.out.print(String.format("File %1$s Exists? %2$s", args[0],f.exists()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM