CSCI 201
Introduction to Algorithm Design home | homework index | labs index |
SPRING 2007 |
In this lab, we're going to look at the problems of file transfer. Pay attention. This information will be helpful in verifying that you have your files in the right places.
The most common reason while file transfer fails is procrastination, waiting until the last minute to work on the homework assignment. If you begin writing your program two days before it is due, you probably will not transfer, or even complete, the assignment on time.
When assignments are given, you will be explicitly told
the name of the Java package and class (or classes) in which your
code should be placed.
For example,
Homework #2 of the Fall 2006 semester specifies that the
package name should be homework2
and
the class name should be Main
.
If you create your program within a NetBeans project called
Homework2, as suggested in the
homework description, your code will by default be placed within
package homework2
and class Main
.
In general, just letting NetBeans do its thing will work fine.
The package and class names are usually set when you go
through NetBeans' New Project...
or New File... "wizards".
In the New Java Application window,
the class and package are both set in a single text field labeled
Create
Main class as packagename.ClassName
.
For example, in the following screen the package name is given
as homeworkxx
and the class name as Main
.
Remember that, by convention, package names contain only lower-case
letters, and class names start with an upper-case letter.
Later in the course, you may add additional classes to
a project with the
New File... "wizard".
In these cases, you may encounter a window in which
you specify the package and class names in different text fields.
This is shown in the following window where the package name is
homeworkxx
and the class name is Payroll
.
/>
The best test for verifying that you have the right package and class name is to look at your program code. The package name should be specified by a line, similar to the following, at the beginning of your program.
package packagename ;
The class name should quickly follow.
public class ClassName {
Always look at these two lines to verify you have the correct package and class names.
If you plan to transfer your files to the UNCA Computer Science server, you will need a file transfer program that uses SSH, the Secure Shell. The protocol to make this transfer is often called "sftp", for Secure FTP.
On a Linux or Mac OS X computer, you can use the command line program sftp to transfer files. We'll show you how to do this later.
The University of Michigan distributes a free, and popular, program called Fugu that is a graphical frontend for sftp for computers running Mac OS X. We suspect most Mac users will prefer the GUI Fugu to the textual sftp.
Many graphical programs for secure file transfer are available for computers running Windows. We recommend you use WinSCP. If you want a good command line interface, you can use the psftp program that is distributed with the PuTTY suite of secure connection programs.
You've been asked to transfer a Java file (or files)
with a specific name, such as ClassName.java
in your assignment.
For now, we're going to assume that the Java file is
Main.java you created for this
project. Most likely, the file was created as part of
a NetBeans project called
HomeworkXX
and is part of a package called homeworkxx
.
If your code was written using NetBeans on the Linux system and you follow the conventions of the labs, most likely your project will be stored in a directory called /home/yourid/csci/201/HomeworkXX. If you are using a Windows systems, it's probably in the directory C:\Documents and Setting\yourid\HomeworkXX.
If you have completely forgotten where your project is stored or if you just want to be absolutely sure; start up NetBeans, open your project, and right-click on the name of your project. This will bring up a Project Properties window which will display the location of your Project Folder. Below you can see images of how your Project Properties window should look on both Linux (first) and Windows (second). Notice the Project Folder textbox. That's the project folder directory.
Linux |
Windows |
We have located the project, but what about that file
Main.java within the package
homeworkxx
?
To determine its absolute name, take a look at the files
panel of NetBeans which should look the same no matter which
operating system you are using.
The above panel shows that Main.java is in a directory called homeworkxx, the package name. In turn, homeworkxx is a subdirectory of src which is a subdirectory of the project directory. The absolute pathname of the file is obtained by concatenating the project directory pathname with the path deduced from NetBeans' file panel. Thus the pathname for the file is /home/yourid/csci/201/HomeworkXX/src/homeworkxx/Main.java in Linux and C:\Documents and Setting\yourid\HomeworkXX\src\homeworkxx\Main.java on Windows.
Admittedly there's a bit of a chicken-and-egg problem here. How can you open a project if you don't know the name of the project folder? You will be given a list of recently opened NetBeans projects when you start NetBeans. Hopefully your project will be one of these.
You'll can also significantly reduce the probability of "losing" a project by storing all your projects in one directory, like csci/201 in Linux.
Now we are going to have you use sftp to transfer a file to the UNCA CSCI file server. This is really a little silly as the file is already stored on the UNCA CSCI file server, but just pretend like you are doing this from a computer located in Bora Bora. Also, keep it mind that if you use NetBeans with the right project directory, package name, and class name from a Linux computer in RH004 this really isn't necessary.
In spite of all this, we continue.
The file we'll use in our example
is the Main.java
of the
Syracuse project of the
Downloading a Lab Project lab.
Start up NetBeans and use the Project Properties window and the Files panel to determine the location of your file.
Ridiculously huge hint: /home/yourid/csci/201/Syracuse/src/syracuse/Main.java .
Using the command line, connect to the directory containing Main.java and list its files.
[yourid@yourmach yourid] cd csci/201/Syracuse/src/syracuse [yourid@yourmach syracuse] ls -l
If you don't see Main.java, call the lab instructor over for a chat.
Assume you have been asked to transfer Main.java to the subdirectory csci/201/HomeworkZZ of your Linux account. Take a look at the commands you should type. An explanation will follow.
[yourid@yourmach syracuse] sftp yourid@ftp.cs.unca.edu Connecting to ftp.cs.unca.edu... The authenticity of host 'ftp.cs.unca.edu (152.18.69.34)' can't be established. RSA key fingerprint is 91:74:a1:75:f7:87:61:5f:ff:9e:1c:5e:da:4e:2a:43. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'ftp.cs.unca.edu' (RSA) to the list of known hosts. yourid@ftp.cs.unca.edu's password: TypeYourPasswordHere sftp> cd csci/201 sftp> mkdir HomeworkZZ sftp> cd HomeworkZZ sftp> put Main.java Uploading Main.java to /HOMEDIR/home/yourid/csci/201/HomeworkZZ/Main.java Main.java 100% nnn n.nKB/s 00:00 sftp> quit [yourid@yourmach syracuse]
The first command, sftp, creates a secure connection to the computer ftp.cs.unca.edu using your account. The remaining commands and output come from the interaction with sftp. The first time you connect to ftp.cs.unca.edu, sftp will not recognize the remote computer's key. You'll be asked if it is OK to connect. You should agree to the connection.
If you are logged in from your home computer, you'll be asked to provide your password. The transmission of the password across the network is encrypted so that no one can capture your password. However, you won't be asked for your password if you're working from the RH004 lab, so don't expect to type one today.
Your first sftp command is to connect to your csci/201. Next, you must create the subdirectory homeworkZZ where your file is to be stored. This is the command that would vary with each homework. You then connect to the newly created subdirectory and use the sftp command put to transfer your file. Now the job is done and you can quit sftp.
Type the commands above and show your instructor that you were able to copy Main.java.
If the homework assignment didn't call for graphical output, you can run the program from a terminal session via a remote connection. Since you were copying to the UNCA CSCI Linux file server, you can easily connect to the target directory and list its files.
[yourid@yourmach yourdir] cd [yourid@yourmach yourid] cd csci/201/HomeworkZZ [yourid@yourmach HomeworkZZ] ls -l
If you don't see Main.java, you need to get the attention of the lab instructor.
The real test would be to actually execute your program. But this
is a little tricky. Because you code is written for the
package syracuse
, the compiled class file must
be stored in a directory call syracuse.
Fortunately, if you run
javac, the Java compiler, with a special
-d option followed by
a period, it will create the directories needed for your Java
packages and place your compiled class files in them.
Try it out by typing the commands below.
[yourid@yourmach HomeworkZZ] javac -d . Main.java [yourid@yourmach HomeworkZZ] ls -lR .: total 8 -rw-r--r-- 1 yourid users 702 Sep 2 18:53 Main.java drwxr-xr-x 2 yourid users 4096 Sep 3 10:21 syracuse ./syracuse: total 4 -rw-r--r-- 1 yourid users 973 Sep 3 10:21 Main.class
The output of your ls -lR command should, as above, show that Main.class is contained within the syracuse directory. It doesn't, it's time for a lab instructor visit.
At last, it's time to execute your program. Once again Java packaging
complicates the task. You must call your program
syracuse.Main
rather than just plain Main
.
[yourid@yourmach HomeworkZZ] java syracuse.Main
Show your instructor that you were able to run the copied program.
By the way, steps like those discussed in this section can be automated with scripts to test your code. We do this when we grade submitted homework assignments. That's why it's important that you transfer your programs to the correct directories and use the right class and package names. If the scripts don't work because you used the wrong directory or name, the instructors get grumpy.
We have an on-line tutorial Copying your code with WinSCP for students who want to copy their programs from their home Windows computer to the UNCA CSCI Linux system.
Last modified: 01/19/07