访问被拒绝,尝试运行简单的Java程序。

huangapple go评论78阅读模式
英文:

Access Denied when trying to run simple Java program

问题

我对Java非常陌生。事实上,我正在尝试运行经典的“Hello World”程序。

我的问题是,当我尝试通过命令提示符运行程序时,我总是收到“拒绝访问”的响应。我已经尝试将我的HelloWorld.java文件移动到计算机的各个位置,但没有一个位置允许程序运行。最初,这是我的执行命令:

C:\Programs>java HelloWorld

这给了我“拒绝访问”的错误。我尝试将程序移动到桌面上的另一个文件夹,如下所示:

C:\Users\Spiralgo\Desktop>java HelloWorld

这仍然给了我“拒绝访问”的错误。我开始在网上进行研究。有人建议程序不能在C:驱动器下运行,可能仅适用于Windows 7,但虽然我使用的是Windows 10,我还是决定尝试遵循这个建议。

我将我的文件(现在有一个名为HelloWorld的.java文件和HelloWorld.class文件,我不知道这个后者是从哪里来的,也不知道它是什么)移动到了D:驱动器,并尝试从那里执行:

D:>java HelloWorld

...然后再次收到“拒绝访问”的错误!

然后我尝试以管理员身份运行命令提示符。仍然收到相同的错误。(我还没有尝试在D:驱动器中以管理员身份运行,但我觉得我真的不应该必须使用D:驱动器。)

关于“拒绝访问”消息的其他信息对我来说太复杂了,我真的希望这里有人可以指点我正确的方向。

英文:

I'm very new to Java. In fact, I'm trying to run the classic Hello World program.

My problem is that when I try to run the program through Command Prompt, I always get a response of "Access is denied." I've tried moving my HelloWorld.java file to various places throughout my computer, but none of them has allowed the program to run. Originally, this was my executing command:

C:\Programs>java HelloWorld

This gave me "Access is denied". I tried moving the program to another folder located on my desktop, as such:

C:\Users\Spiralgo\Desktop>java HelloWorld

And this gave me "Access is denied." I started researching online. Someone suggested that programs cannot be run under the C: drive, possibly only for Windows 7, but while I have Windows 10, I thought I'd try to follow the advice.

I moved my file(s) (there is now HelloWorld as a .java file and HelloWorld.class, I don't know where this latter program came from or what it is) to my D: drive and tried executing through there:

D:>java HelloWorld

...And again got "Access is denied." !

Then I tried running Command Prompt as an Administrator. Still got the same error. (I have not yet tried running as Administrator within D: drive, but I feel like I shouldn't really have to use D: drive.)

The rest of the information I've found on the "Access is denied." message has been too complicated for me to understand. I really hope someone here can point me in the right direction.

答案1

得分: 0

尝试,在C:\Users\Spiralgo\Desktop目录下:

将你的HelloWorld.java文件放在这里。
运行 javac HelloWorld.java,这会编译该文件,生成一个HelloWorld.class文件。
运行 java HelloWorld,这会执行HelloWorld.class文件。

英文:

Try, from C:\Users\Spiralgo\Desktop:

Put your HelloWorld.java file here.
Run javac HelloWorld.java, which compiles the file, creating a
HelloWorld.class file.
Run java HelloWorld, which runs the HelloWorld.class file.

答案2

得分: 0

我成功让它运行了!基本上看起来它没有工作是因为我没有给出文件所在的确切文件夹。以下是基本上成功的代码:

C:\Users\Spiralgo\Desktop\Coding>java HelloWorld

然后得到:Hello world!

我以为 "Desktop" 应该足够具体,根据我在其他地方看到的示例,但事实证明我必须包括程序所在的确切文件夹。我不知道为什么当我把它放在 "Programs" 文件夹(或者 D 盘)时它不起作用,但我已经足够满意。谢谢大家。

英文:

I got it to work! Basically, it looks like it wasn't working because I didn't give the exact folder the file was in. Here was basically the successful code:

C:\Users\Spiralgo\Desktop\Coding>java HelloWorld

And got: Hello world!

I thought Desktop would be specific enough based on examples I'd seen elsewhere, but turns out I had to include the exact folder that the program was in. I don't know why it didn't work when I had it in the Programs folder (or D:), but I'm satisfied enough. Thanks everyone.

huangapple
  • 本文由 发表于 2020年9月13日 02:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63863425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定