-
Java Problems - JDK
Hey,
I got a Java programming book, and I'm having some trouble. I got the Sun J2SE JDK, with the NetBeans IDE. NetBeans it wonderful, and every file compiles perfectly. But, if I compile manually, (with javac command,) I get errors. I'm using java.util.Scanner to recieve keyboard input, and this is the error I get when compiling the source manually:
Code:
[admin@DellLin classes]javac KeybdInput.java
----------
1. ERROR in KeybdInput.java
(at line 1)
import java.util.Scanner;
^^^^^^^^^^^^^^^^^
The import java.util.Scanner cannot be resolved
----------
2. ERROR in KeybdInput.java
(at line 20)
Scanner ScannerOne = new Scanner(System.in);
^^^^^^^
Scanner cannot be resolved to a type
----------
3. ERROR in KeybdInput.java
(at line 20)
Scanner ScannerOne = new Scanner(System.in);
^^^^^^^
Scanner cannot be resolved to a type
Can you give me some insight as to why if I compile with NetBeans it's ok, and now it's not with javac?
Thanks,
Firewing1
-
If you have installed the netbeans package including the sun jdk then it might be jdk v5 = v1.5. Do you use javac from sun jdk or from the jdk provided by fedora? I do not know at the moment, if fedoras jdk (1.4.2) supports java.util.Scanner...? On the other side it looks like a CLASSPATH-problem... It seems that the compiler could not find the java.util-package.
-
rt.jar?
I downloaded JDK .bin file from Sun site b/c of the warning I saw about the Java RPM. I usually use NetBeans to compile, and it works OK. I just wanna be able to program .java sources, and make sure they can compile with javac because I try to make my program load as an applet, the jvm runs, takes 100 % CPU for about 2 seconds (small program), then Firefox hangs. If I run it with java myapplet.class, it still messes up. I think it's cause the source won't compile with javac in the first place.
Plus, I went under "Libraries" in my IDE, and rt.jar is there. I looked, and sure enough, the java.util.Scanner is there. So, I know I have the file, I'm just not sure if it's loaded...
Firewing1
-
It seems like you are not using Sun's JDK 5.0 when compiling manually. Because Scanner was introduced only with 5.0. Installing Sun's JDK/JRE: First get the latest version from http://java.sun.com. Download the "Linux self extracting file" not the rpm package. After download, become root, move the file to /opt
mv jdk-1_5_0_04-linux-i586.bin /opt
change directory to /opt
cd /opt
change the mod of the file to executable,
chmod +x jdk-1_5_0_04-linux-i586.bin
and then execute the program
./jdk-1_5_0_04-linux-i586.bin
After install succeds, you should tell your system that the Sun's JDK/JRE is your primary java platform, not the gcc-java which is the default package in FC4. Create a symbolic link
ln -s jdk1.5.0_04 jdk1.5
install the new JDK as java alternatives
/usr/sbin/alternatives --install /usr/bin/java java /opt/jdk1.5/bin/java 2 --slave /usr/bin/javac javac /opt/jdk1.5/bin/javac --slave /usr/bin/javadoc javadoc /opt/jdk1.5/bin/javadoc --slave /usr/bin/jar jar /opt/jdk1.5/bin/jar
configure the java alternatives
/usr/sbin/alternatives --config java
choose the newly installed JDK as the primary choice. Check the alternatives to javato make sure
/usr/sbin/alternatives --display java
Because of the symbolic link, you don't need to install new alternatives each time you install an update to JDK/JRE. For browser (firefox) plugins to work, do the following as root
ln -s /opt/jdk1.5/jre/plugin/i386/ns7/libjavaplugin_oji.so /usr/lib/mozilla/plugins/libjavaplugin_oji.so
-
Hey,
Thanks for the info. I havn't tried it yet, as I'm currently not at my home computer. I did get the correct .bin, and did install it to /opt, but I didn't do all the other linking and config. I'll try that when I get to my computer and let you know :)
Firewing1
-
Thanks a ton! I can now compile things properl... I'm gonna bookmark this for future reference... Wow this gcc java seems to be causing lots of problems! Why not just remove it, and have an application that notifies you that certain applications on your system require java, and that you should download it... It would be much better that gcc java....
EDIT: OK, another problem :confused: I get this whenever I try to run a java script form terminal:
Code:
# java -classpath . Key*.class
Exception in thread "main" java.lang.NoClassDefFoundError: KeybdInput/class
Firewing1
-
BTW, this is my code, if it helps. (Don't ask. It's a online fruit store that gives you prices. It was just for practice, as I'm new to java. This won't serve any purpose... lol)
Code:
import java.util.Scanner;
/*
* KeybdInput.java
*
* Created on June 28, 2005, 7:14 PM
*
* To change this template, ch oose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
/**
*
* @author admin
*/
public class KeybdInput {
/** Creates a new instance of KeybdInput */
public static void main(String args[]) {
Scanner ScannerOne = new Scanner(System.in);
System.out.println("Welcome to the Online FruitStore!");
System.out.println("1 = Grapes");
System.out.println("2 = Apples");
System.out.println("3 = Oranges");
System.out.println("4 = Apricots");
System.out.println("5 = Bananas");
System.out.println("What would you like to buy?");
int wantbuy = ScannerOne.nextInt();
double price;
int manybuy;
switch (wantbuy) {
case 1:
System.out.println("Grapes cost $3 per grapevine branch" +
"(A branch contains about 150 grapes).");
System.out.println("How many do you want? (Write as a number. EG: 5)");
manybuy = ScannerOne.nextInt();
price = manybuy * 3.00;
System.out.print("The total cost for " + manybuy
+ " grapevine branch(es) is $" + price);
break;
case 2:
System.out.println("Apples cost $2 per bag. (5 apples par bag.)");
System.out.println("How bags do do you want? (Write as a number. EG: 5)");
manybuy = ScannerOne.nextInt();
price = manybuy * 2.00;
System.out.print("The total cost for " + manybuy + " apple bag(s) is $" + price);
break;
case 3:
System.out.println("Oranges cost $0.75 each.");
System.out.println("How many do you want? (Write as a number. EG: 5)");
manybuy = ScannerOne.nextInt();
price = manybuy * 0.75;
System.out.print("The total cost for " + manybuy + " orange(s) is $" + price);
break;
case 4:
System.out.println("Apricots cost $2.50 per bag. (50 apricots par bag)");
System.out.println("How many do you want? (Write as a number. EG: 5)");
manybuy = ScannerOne.nextInt();
price = manybuy * 2.50;
System.out.print("The total cost for " + manybuy + " apricot bag(s) is $" + price);
break;
case 5:
System.out.println("Bananas cost $1 per bunch. (5 Bananas)");
System.out.println("How many do you want? (Write as a number. EG: 5)");
manybuy = ScannerOne.nextInt();
price = manybuy * 1.00;
System.out.print("The total cost for " + manybuy
+ " banana bunch(es) is $" + price);
break;
}
}
}
Firewing1
-
You run java programs like this:
java KeybdInput
No .class at the end.
-
Thanks - It worked!
Okay, now, I can compile and view my programs but my applets won't work. I put appletviewer myapplet.class and my promt just re-appears. No windows or anything. If i view in firefox, it hangs with 100% CPU.
Firewing1
-
Any reason for my applet to hang Firefox?
EDIT: OK, took example off Sun site and pasted it into a document, then compiled it. It still hangs Firefox. The same applet, if viewed on the Sun site, works. Do you think it's because my url is http://localhost/applet.php? (It's 127.0.0.1?)
Thanks,
Firewing1
-
bump.........................................
-
Solved. It seems applets can't handle the Scanner plugin / Import. Firefox wouldn't hang if I commented out the line asking for the user to input.
Firewing1
-
Oh yes, see showInputDialog at http://java.sun.com/docs/books/tutor...log.html#input . That is the best way to quickly get user input in applets or even in applications.
-
-
I highly recommend you use netbeans because it handles all (okay, most) the plumbings.