LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2011, 01:35 AM   #1
IceTherapy
LQ Newbie
 
Registered: Dec 2007
Location: Portland, OR
Distribution: debian squeeze 6.0.1a i386
Posts: 5

Rep: Reputation: 0
New Java user stuck on NumberFormatException and NoClassDefFoundError


I'm a noob linux user who thought it'd be pretty sweet to take an intro to programming prerequisite course for a master's program on a linux based system. Made sense at the time, use on operating system based on the premise that you'll be using the command line frequently to take a course where you'll be running your programs from the command line. Around the end of week two, and I started having some serious issues w/ my programs. Basically, any program that has to do any type of calculation (rather than just print string values I set initially for their respective variables), I get two intermittant errors.

Exception in thread "main" java.lang.NumberFormatException: invalid character at position 1 in uninitializedValue
at java.lang.Integer.parseInt(libgcj.so.10)
at java.lang.Integer.parseInt(libgcj.so.10)
at CollegeAdmission.main(CollegeAdmission.java:21)
icetherapy@Frodo:~/Documents/intro_prog/smith_Ch3$

The error is so intermittant I can't actually tell you what criteria to provide to get it. Numbers under 50 seem to trigger it more often though. I'll post the actual java code for that particular file in a second post. There is a second error I haven't been able to reproduce as a standard user tonight, but I got it to come up logged in as root:

Exception in thread "main" java.lang.NoClassDefFoundError: CollegeAdmission.
at gnu.java.lang.MainThread.run(libgcj.so.10)
Caused by: java.lang.ClassNotFoundException: CollegeAdmission. not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./,file:/home/icetherapy/Documents/intro_prog/smith_Ch3/], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.10)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.10)
at java.lang.ClassLoader.loadClass(libgcj.so.10)
at java.lang.ClassLoader.loadClass(libgcj.so.10)
at gnu.java.lang.MainThread.run(libgcj.so.10)


Things I've tried to do to fix it:

Entered the following into my .bashrc profile in an attempt to set the classpath. I spotted the JAVA_HOME bit in a thread and thought it worth a shot since I've been at this on and off for over two weeks with no success

# 5th attempt at adding CLASS_PATH to .bashrc profile
export CLASSPATH=$CLASSPATH:~/Documents/intro_prog/smith_Ch3
JAVA_HOME=~/etc/java-6-sun export JAVA_HOME

Things I think I may have screwed up:

This is on Debian Squeeze, I went with a simple non-partitioned graphical install, and installed the JDK using they synaptic package manager. Normally, it looks like people have Java installed in their /bin directory, but mine ended up in /etc after the automated install

pwd returns: /etc/java

The class definition error makes me think that it's something to do with my CLASSPATH variable, but even when I set it in .bashrc to the directory the .class files are housed in, that error still returns. I feel like the integer parse error has something to do with not having installed everything, but I'm really just making that up, as I have no definitive proof.

I will say that it's been kind of fun to troubleshoot, and that I've been learning a lot mashing through man pages and the Intro to Linux guide, but I'm starting to get a bit behind in my coursework so I'm throwing in the towel and asking for a bit of help :P

Tried everything I could from this thread and those linked @ the bottom
 
Old 07-19-2011, 01:36 AM   #2
IceTherapy
LQ Newbie
 
Registered: Dec 2007
Location: Portland, OR
Distribution: debian squeeze 6.0.1a i386
Posts: 5

Original Poster
Rep: Reputation: 0
Here's the Java code

/* Program Name: CollegeAdmission.java
Function: This program determines if a student will be admitted or rejected.
Input: Interactive
Output: Accept or Reject
*/

import javax.swing.JOptionPane;
public class CollegeAdmission
{
public static void main(String args[])
{
// Declare variables
String testScoreString;
String classRankString;
int testScore;
int classRank;

// Get input and convert to correct data type
testScoreString = JOptionPane.showInputDialog("Enter Test Score");
classRankString = JOptionPane.showInputDialog("Enter Class Rank");
testScore = Integer.parseInt(testScoreString);
classRank = Integer.parseInt(classRankString);

// test using admission requirements and print Accept or Reject
if( testScore >= 90 )
{
if( classRank >= 25)
{
System.out.println("Accept");
}
else
System.out.println("Reject");
}
else
{
if( testScore >= 80 )
{
if( classRank >= 50 )
System.out.println("Accept");
else
System.out.println("Reject");
}
else
{
if( testScore >= 70 )
{
if( classRank >=75 )
System.out.println("Accept");
else
System.out.println("Reject");
}
else
System.out.println("Reject");
}
}
System.exit(0);


System.exit(0);
} // End of main() method

} // End of CollegeAdmission class
 
Old 07-19-2011, 04:10 PM   #3
indelible
LQ Newbie
 
Registered: Aug 2008
Posts: 25

Rep: Reputation: 2
I would suggest your NumberFormatException is caused by whitespace. Try calling trim() on the results of your JOptionPane.showInputDialog() String.

As for the rest... Linux is different to windows in that it is inherently is a multi-user environment. It is strongly suggested that if you don't need to be running as root, to not. Have your working directory as a sub directory of your home (~) dir, so filesystem permissions aren't a problem. java* should be on your $PATH and you should change that if you can't access it by just typing 'java' on the command line.

If you run your program as root, then it has different settings to "your" .bashrc (with very good reason!) and it is *good* that it doesn't 'just work'. There is no reason in the world why you need to run the above program as root. Doing so is irresponsible and clearly The Wrong Thing and will come back to bite you if you start getting into such a habit and have a program that cleans out the pwd (I have done this, I know from experience that learning the correct way is better!).

Other than that, good luck with your programming!!
 
Old 07-19-2011, 11:04 PM   #4
IceTherapy
LQ Newbie
 
Registered: Dec 2007
Location: Portland, OR
Distribution: debian squeeze 6.0.1a i386
Posts: 5

Original Poster
Rep: Reputation: 0
Added this to the .java file:

String testScoreStringTrim = testScoreString.trim();
String classRankStringTrim = classRankString.trim();

And it worked! For a little while... I was able to put pretty much any number I wanted into the program for about the first six tries, then the NumberFormatException error came back. Where would this extra whitespace be coming from? Is is the actual spaces and returns that I place in the file to keep things organized?

I realize running in root was an issue, I was only trying it temporarily to see if I could get more information on the error. Based on your description, it makes sense why it couldn't find the Class definitions, because they weren't set on the root profile, so that's helpful.

Java does run directly off the command line, but I think you're onto something when you say my working directory should be in home and not buried deep into some folders. Should I uninstall java and redo the installation manually to drop it into the usr/bin directory instead of the /etc directory? It seems like it shouldn't make a difference if I set my PATH and CLASSPATH variables appropriately.
 
Old 07-20-2011, 11:26 PM   #5
indelible
LQ Newbie
 
Registered: Aug 2008
Posts: 25

Rep: Reputation: 2
The Whitespace in question is being returned by the JOptionPane method. Unless you're very careful when you're typing in your numbers, it's very easy to add whitespace by accident.

Effectively, you need some more information - i.e. what about the string wasn't a number? Here's how I would debug the situation (without physically attaching a debugger, which might be useful too...)

Code:
import javax.swing.JOptionPane; 
public class CollegeAdmission
{
   public static void main(String args[])
   { 
      // Declare variables
      String testScoreString;
      String classRankString;
      int testScore;
      int classRank;

      // Get input and convert to correct data type
      testScoreString = JOptionPane.showInputDialog("Enter Test Score").trim();
      classRankString = JOptionPane.showInputDialog("Enter Class Rank").trim();

      try
      {
         testScore = Integer.parseInt(testScoreString);
         classRank = Integer.parseInt(classRankString);
      }
      catch (NumberFormatException nfe)
      {
         System.out.println("S" + testScoreString + "E");
         System.out.println("S" + classRankString + "E");
      }
   }
}
That way, you can see what's in the string and figure out why the parser is barfing. Also, remember that it's an Integer, so floating point numbers won't work.

As for your installation, I'm not sure how it ended up in /etc - that folder is traditionally used for configuration, but installing it again (make it easy on yourself and install it through apt-get (if sun java isn't available in the Debian repositories by default, then use openJDK - it's _nearly_ as good!) would be a good idea.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another java classpath question --- java.lang.NoClassDefFoundError Jama raequin Programming 1 12-29-2009 01:04 PM
Java NumberFormatException with a valid number/string manolakis Programming 1 12-14-2008 04:59 AM
java.lang.NoClassDefFoundError robbbert Programming 6 07-16-2006 06:47 AM
Java: java.lang.NoClassDefFoundError: javaapplication8/Factorial chief_officer Programming 9 05-31-2006 03:28 PM
java exception: java.lang.NoClassDefFoundError kath Programming 12 05-11-2006 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration