From leavens@larch.cs.iastate.edu Wed Jan 15 19:42:05 2003 Date: Wed, 15 Jan 2003 19:41:20 -0600 (CST) From: Gary T. Leavens To: Carl Subject: Re: CLASSPATH & JVM problems On Wed, 15 Jan 2003, Carl wrote: > I am having trouble changing the classpath variable on the department > machines. I am currently logged into popeye and I am unable to change the > variable, any help you could provide would be greatly appreciated. It might help if you could describe the problem in more detail. But I'm guessing that the problem is that you are using tcsh instead of bash as your shell. If that's the case, then doing what I showed in class won't work, and you should do something like (where % is the tcsh prompt): % setenv CLASSPATH '.:/home/ugrad/carl/cs362' or something like that to put the directories . and /home/ugrad/carl/cs362 into your CLASSPATH. Take a look at the running java web page, which is off the course web page in case my guess as to what you're having trouble with is wrong. > Also, in attempting to use the eclipse software on my personal computer > running Windows XP I run into an error telling me that it cannot find the Java > Virtual Machine. I also encounter the same error when attempting to install > j2sdkee 1.4. Due to my inexperience with java, I am a bit slow on these > matters, I apologize for my ignorance. No problem. You first need to get Java J2SDK 1.4 installed before you can install eclipse. If you're trying to install the "enterprise edition" of Java (which you don't need all of), you should install the standard edition first. Does that help? -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ---------------------------------------------------------- From leavens@cs.iastate.edu Sat Jan 18 06:06:04 2003 Date: Sat, 18 Jan 2003 05:48:20 -0600 (CST) From: Gary T. Leavens To: David A. Avila Cc: Staff for Com S 362 Subject: Re: Question hw1 Hi David, On Fri, 17 Jan 2003, David A. Avila wrote: > What is the criteria for sorting the lines??? Is it by the number of > characters in the line or the first character in the line or which one?? Thank > you. Sorry for the confusion. As the homework says, these are to be sorted as Strings, using lexicographic ordering. That is, a string s1 is considered less than a string s2 if the first n>=0 characters of s1 and s2 are the same, but the n+1th character of s1 is less than the n+1th character of s2, or if s1 has no n+1th character but s2 does. Thus the first character on the line counts the most. This was confusing becuase in the homework file (hw1.txt) there were some tab characters that I didn't see in the example, which caused the output to look wierd. I have just now fixed this in the statement of homework 1. To sort, you should use java.util.Collections.sort, which will do the right thing; you shouldn't need to write the code yourself. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------------------------- From leavens@cs.iastate.edu Mon Jan 20 03:52:58 2003 Date: Mon, 20 Jan 2003 03:52:32 -0600 (CST) From: Gary T. Leavens To: Brian Dexter Allen Cc: cs362s@cs.iastate.edu Subject: Re: HW1: Method of sorting, Col.sort vs SortedSet Hi Brian, On Sun, 19 Jan 2003, Brian Dexter Allen wrote: > In the Q/A for HW1 (http://www.cs.iastate.edu/~cs362/q_and_a.txt), Prof. > Leavens suggests, "To sort, you should use java.util.Collections.sort." > > I'm not using Col.sort, I'm instead adding the Strings to a SortedSet. Note > that they get sorted by the default String comparator (just like Col.sort would- > - same ordering). Is this OK, or is using Col.sort part of the assignment > requirements? I believe that a SortedSet will discard duplicate Strings. Thus it won't fulfill the requirement that: "The file may contain duplicate lines, and these duplicates should be preserved in the output." If it preserves duplicate lines, then that's okay, but my guess is that will be wrong. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------------------------------------------- From leavens@cs.iastate.edu Mon Jan 20 11:51:20 2003 Date: Mon, 20 Jan 2003 11:50:33 -0600 (CST) From: Gary T. Leavens To: kwendzel@iastate.edu Cc: Staff for Com S 362 Subject: Re: ComS 362 assignment Kwendzel, On Mon, 20 Jan 2003 kwendzel@iastate.edu wrote: > I'm having problems running my code, and it's probably a CLASSPATH error and I > don't know how to solve it. I've finally got SortLines.java program to > compile in Windows XP using the JavaTM 2 Platform, Standard Edition v1.4.1 > (using javac.exe), but now when I try to run it (using java.exe), I keep > getting the error "Exception in thread "main" > java.lang.NoClassDefFoundError:". How do I stop this? I've tried setting my > classpath to everything imaginable, and nothing works. It would help if you could send all of the details on the error you're getting. But let me hazard a guess. It might be that you have a difference in case between the file name and the class name. For example, if you wrote public class SortLines { ... } but you put it in a file named "sortlines.java" that would cause this problem. Java is very case-sensitive, even on Windows. If that isn't it, look at the Running Java web page (in the troubleshooting section especially) and give it your best shot, then send us *all* of the output you're getting and what your CLASSPATH is set to. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------------------------------ From leavens@larch.cs.iastate.edu Mon Jan 20 14:11:05 2003 Date: Mon, 20 Jan 2003 14:09:37 -0600 (CST) From: Gary T. Leavens To: kwendzel@iastate.edu Subject: Re: ComS 362 assignment Hi Kevin, (sorry I got your name wrong before) On Mon, 20 Jan 2003 kwendzel@iastate.edu wrote: > The file name and the class name are exactly the same. > > Here is the output I get after each try of running java.exe: > > H:\j2sdk141\bin>java e:\school\programs\SortLines > Exception in thread "main" java.lang.NoClassDefFoundError: > e:\school\programs\SortLines Ok, thanks. There are at least two problems. First, your code must be found in a directory named "cs362hw" for the package declaration package cs362hw; at the beginning of SortLines.java to work right. Second, when you run the "java" command, you should type java cs362hw.SortLines You don't give the command "java" the entire path to the file, you should only give it the fully qualified name of the class. A third potential problem is that if the full path to your program code is e:\school\programs\cs362hw\SortLines.java and so the .class file produced by is e:\school\programs\cs362hw\SortLines.class then your CLASSPATH (or -classpath option to java) must contain e:\school\programs\ But this only works if you have the right package statement and the right file locations. If your class doesn't start with the package declaration (so, incorrectly, it's in the unnamed package), then you would run the java interpreter using "java SortLines" and again you wouldn't give the full path name. One that should work is the following: H:\j2sdk141\bin>java -classpath E:\School\Programs\ cs362hw.SortLines but only if you have the cs362hw directory or if you have left out the package declaration (incorrectly). If you have the right package declaration, you can't do: > H:\j2sdk141\bin>java -classpath E:\School\Programs\ SortLines > Exception in thread "main" java.lang.NoClassDefFoundError: SortLines because the fully qualified name of the class is cs362hw.SortLines, not SortLines. All the other attempts you sent have one of the problems I mentioned above. By the way, these issues are much easier to deal with (they usually vanish) if you use Eclipse. Hope this helps. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------------------------------------------------- From leavens@cs.iastate.edu Mon Jan 20 14:16:15 2003 Date: Mon, 20 Jan 2003 14:14:27 -0600 (CST) From: Gary T. Leavens To: kwendzel@iastate.edu Cc: Staff for Com S 362 Subject: Re: ComS 362 assignment Kevin, On Mon, 20 Jan 2003 kwendzel@iastate.edu wrote: > Here is an error I get when I try something else: > > E:\School\Programs>java.exe -classpath . SortLines > Exception in thread "main" java.lang.NoClassDefFoundError: SortLines (wrong > name > : cs362hw/SortLines) > at java.lang.ClassLoader.defineClass0(Native Method) > at java.lang.ClassLoader.defineClass(ClassLoader.java:502) > at java.security.SecureClassLoader.defineClass > (SecureClassLoader.java:123) > at java.net.URLClassLoader.defineClass(URLClassLoader.java:250) > at java.net.URLClassLoader.access$100(URLClassLoader.java:54) > at java.net.URLClassLoader$1.run(URLClassLoader.java:193) > at java.security.AccessController.doPrivileged(Native Method) > at java.net.URLClassLoader.findClass(URLClassLoader.java:186) > at java.lang.ClassLoader.loadClass(ClassLoader.java:299) > at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265) > at java.lang.ClassLoader.loadClass(ClassLoader.java:255) > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315) Right, this means that what I said in my previous email is right. You have a package statement (correctly in SortLines.java), that reads "package cs362hw", but you aren't passing the fully qualified name "cs362hw.SortLines" to the Java interpreter (and you don't have the file in the right directory, it has to live in E:\School\Programs\cs362hw\). -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 --------------------------------------------------------- From leavens@cs.iastate.edu Mon Jan 20 17:16:49 2003 Date: Mon, 20 Jan 2003 17:16:20 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: Staff for Com S 362 Subject: Re: Hw1 (syntax error) Hi Emily, On Mon, 20 Jan 2003, Emily Ericson wrote: > I copied my code at the bottom of the message. I looked for errors before the > curly brace in question which I marked, and tried removing things that I > thought might be causing it but I couldn't figure it out. Thanks for giving us that information. The problem is that you wrote "While" with a capital "W" instead of the correct Java keyword "while" with a lowercase "w". Keywords in Java, like all other Java syntax, are case-sensitive. Are you trying to use pico as your editor? If you used Eclipse, you would see this right away, as it highlights keywords. Try using Eclipse instead. > I was also wondering > how we are handing in our homework? Thanks. See the homework itself, which says that you are supposed to make us a printout. Does that help? > While(!input.eof()){ <--this is where I'm getting the Error > > While (Current != '\n'){ I also note that you're not using much of the capabilities of the JDK. You should use a LineNumberReader instead of a raw DataStream, etc. See the "HINTS" section of the homework. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 --------------------------------------------------- From leavens@cs.iastate.edu Tue Jan 21 12:13:40 2003 Date: Tue, 21 Jan 2003 12:11:31 -0600 (CST) From: Gary T. Leavens To: Emily Ericson Cc: cs362s@cs.iastate.edu Subject: Re: Hw1, calling Collections.sort() Hi Emily, On Tue, 21 Jan 2003, Emily Ericson wrote: > I am having a problem with the sort command. I keep getting an error message > that says it is undefined within java.util.ArrayList. Also in trying to add > java.util.Collections.sort I get an error saying that it is not recognized. > Is there anything specific that might be causing these errors? You have to write import java.util.Collections; ... Collections.sort(myArrayList); ... or equivalently (without the import): ... java.util.Collections.sort(myArrayList); ... to sort using Collections.sort, which is a static method. Being static means you can't write: myArrayList.sort(); and you can't write myArrayList.Collections.sort(); and you can't write myArrayList.java.util.Collections.sort(); I'm not sure exactly what you mean otherwise. If that doesn't help, can you please send us both all the details of the error message output and the program so we can see more details? -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 -------------------------------------------------- From leavens@cs.iastate.edu Thu Jan 23 02:56:54 2003 Date: Thu, 23 Jan 2003 02:56:26 -0600 (CST) From: Gary T. Leavens To: Carl Cc: Staff for Com S 362 Subject: Re: COMS 362: Homework 2 is ready for 362 Hi Carl, On Thu, 23 Jan 2003, Carl wrote: > I am still working on hw1 and I am having some trouble. I am using a > lineNumberReader to read in lines as strings and store them as strings in a > collection. This works for a given number of lines, but i have no way of > consuming the entire file. I cannot find a EOF method in the class. Also i am > having many problems with writing to a new file which I created. I declared a > printwriter and attemped to use it, but was unable to make it work. Any help > would be appreciated. Try using the "ready()" method, as in: while (in.ready()) { ... in.readLine()... } You're right that there is no "eof" method in this class. To find the "ready" method, I looked at an example in the Java Programming Langauge (3rd ed) book I recommended for the class. You can also find it in the javadocs, for LineNumberReader, but you have to go to the "inherited" methods sections, and then you can see the method in BufferedReader. Let me know if that doesn't help. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------------------------------- From leavens@cs.iastate.edu Thu Jan 23 04:35:58 2003 Date: Thu, 23 Jan 2003 02:56:26 -0600 (CST) From: Gary T. Leavens To: Carl Cc: Staff for Com S 362 Subject: Re: COMS 362: Homework 2 is ready for 362 Hi Carl, On Thu, 23 Jan 2003, Carl wrote: > I am still working on hw1 and I am having some trouble. I am using a > lineNumberReader to read in lines as strings and store them as strings in a > collection. This works for a given number of lines, but i have no way of > consuming the entire file. I cannot find a EOF method in the class. Also i am > having many problems with writing to a new file which I created. I declared a > printwriter and attemped to use it, but was unable to make it work. Any help > would be appreciated. Try using the "ready()" method, as in: while (in.ready()) { ... in.readLine()... } You're right that there is no "eof" method in this class. To find the "ready" method, I looked at an example in the Java Programming Langauge (3rd ed) book I recommended for the class. You can also find it in the javadocs, for LineNumberReader, but you have to go to the "inherited" methods sections, and then you can see the method in BufferedReader. Let me know if that doesn't help. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ----------------------------------------------------- From leavens@cs.iastate.edu Thu Jan 23 11:39:03 2003 Date: Thu, 23 Jan 2003 11:35:28 -0600 (CST) From: Gary T. Leavens To: Carl Cc: Staff for Com S 362 Subject: Re: no output appears in file for HW1. Hi Carl, On Thu, 23 Jan 2003, Carl wrote: > Thank you for showing me the ready method, it helped in reading the lines in. I'm glad that worked helped. > I am still confused about how to write lines out. I can create a new output > file and declare a printwriter object for it, but when i use the various > methods for the printwriter nothing happens. I am left with an empty output > file at the end of my program run. Any ideas? Yes, be sure to close the file. Perhaps I mentioned this in class when I discussed exception handling, but closing or flushing is necessary to make sure the contents get out to the file. (Close calls flush, and also releases the OS resources, so it's better.) To be sure of this, use a "finally" clause in a try-catch-finaly statement. As an aside, this is important for debuging output as well; if you write to a buffered stream and don't flush, and then your program exits (e.g., aborts, then you have no output or incomplete output). So it's an important concept. -- Gary T. Leavens Department of Computer Science, Iowa State University 229 Atanasoff Hall, Ames, Iowa 50011-1041 USA http://www.cs.iastate.edu/~leavens phone: +1-515-294-1580 ------------------------------------------