How to save and run a Java file in PuTTY?

2019-02-20 05:01发布

问题:

I m a complete newbie on AWS(Amazon Web Services) so this might be a basic question.

I created an EC2 instance on AWS. I have a windows machine so I'm using PUTTY to connect with the Linux instance.

After getting connected with my EC2 instance, I used the below command to write the Java code,

vi java First.java

After writing the above code, I got the below screen and I wrote the Java code for a simple "Hello World",

The problem is now I'm stuck here, what should I do to save what I have written? Where it will be stored, in my laptop or on the EC2? Is there a way through which I can simply upload the file and then just run it from the terminal?

Thanks in advance.

回答1:

I am using putty since almost 2 years..

U should open the vi editor using "vi filename.java"

Type the program and then say "Esc : wq" ignore the spaces.. And Esc is the Esc button on keyboard..

Then compile your program using "javac filename.java"

N then run using java filename.java



回答2:

See Saumil..

When you login then the directory in which you are landed is your users home directory.. Like in your case "ec2-user"..

So if you do "vi filename" without changing directory then the file is created in the users home directory "/home/ec2-user" .. N that is represented by ~.. Which represents the current users home directory..

N then if you logout and login again then the file stays there.. Nothing happens to that file as long as you manually delete it..

N by the above snapshot I can see that after login you are changing your user to root.. But if you could see after doing sudo you still remain in the same directory. N "~" changes to "ec2-user" because the root users home directory is different..