Simple Java program

If even simple console can't be created, where would be the problem? Only when I am in eclipse

  1. import java.io.Console;
  2. import java.sql.SQLException;


  3. public class MainDocument {
  4.   public static void main(String[] args) throws ClassNotFoundException, SQLException {
  5.     Console console = System.console();
  6.     if (console == null) {
  7.       System.err.println("sales: unable to obtain console");
  8.       return;
  9.     }

  10.     console.printf("%s ", "string");
  11.   }
  12. }
複製代碼

result: sales: unable to obtain console




I have set the path X:\Sun\sdk\jdk\bin to no avail.
Any suggestions?

[ 本帖最後由 luckiejacky 於 2009-4-1 17:56 編輯 ]

原帖由 luckiejacky 於 2009-4-1 17:53 發表
If even simple console can't be created, where would be the problem? Only when I am in eclipse


import java.io.Console;
import java.sql.SQLException;


public class MainDocument {
  public static vo ...



唔明你句英文寫乜野....

TOP

Sorry.... I meant when I am running the application, the result "Sales: unable to get console" is shown.
I guess there is some configuration problem, and I forgot which...
When i run the application under command prompt, it is okay
  1. java myApp
複製代碼

but fail when I run under eclipse...
  1. Run >> run as java application
複製代碼

I have reboot the system, the same results came up

Another symptom is I can run the application as applet, but not application...
Another complement, when I run it using javaw.exe on the command prompt, nothing is displayed

Does anyone know why this happens?
  1. Console console = System.console();
複製代碼

After the call, console is null



Thanks

[ 本帖最後由 luckiejacky 於 2009-4-1 21:02 編輯 ]

TOP

I had these errors when I compiled another set of application

  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2.         at CardLayoutFun3.getCardPanelInfo(CardLayoutFun3.java:58)
  3.         at CardLayoutFun3.<init>(CardLayoutFun3.java:40)
  4.         at CardLayoutFun3.createAndShowUI(CardLayoutFun3.java:218)
  5.         at CardLayoutFun3.access$2(CardLayoutFun3.java:214)
  6.         at CardLayoutFun3$2.run(CardLayoutFun3.java:232)
  7.         at java.awt.event.InvocationEvent.dispatch(Unknown Source)
  8.         at java.awt.EventQueue.dispatchEvent(Unknown Source)
  9.         at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  10.         at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  11.         at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  12.         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  13.         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  14.         at java.awt.EventDispatchThread.run(Unknown Source)
複製代碼


So does anyone know what's going on?
Thanks

TOP

please read java se api the Console section(java.io.Console), then you will know why.

TOP

Hello, what would be the general causes of these messages
  1. Exception in thread "main" java.lang.NoClassDefFoundError: test
  2. Caused by: java.lang.ClassNotFoundException: test
  3.         at java.net.URLClassLoader$1.run(Unknown Source)
  4.         at java.security.AccessController.doPrivileged(Native Method)
  5.         at java.net.URLClassLoader.findClass(Unknown Source)
  6.         at java.lang.ClassLoader.loadClass(Unknown Source)
  7.         at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  8.         at java.lang.ClassLoader.loadClass(Unknown Source)
  9.         at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  10. Could not find the main class: test.  Program will exit.
複製代碼

Under dos command , and I have constructor
and how do i fix it? I've google, couldn't find the appropriate source
Thanks

[ 本帖最後由 luckiejacky 於 2009-4-7 21:20 編輯 ]

TOP

原帖由 luckiejacky 於 2009-4-7 21:12 發表
Hello, what would be the general causes of these messages

Exception in thread "main" java.lang.NoClassDefFoundError: test
Caused by: java.lang.ClassNotFoundException: test
        at java.net.URLClas ...


你邊度有個"test" 既class.

TOP

原帖由 thinkpanda 於 2009-4-7 23:37 發表


你邊度有個"test" 既class.


Hello Panda Ching,
Sorry I did not mention, that is another program... I have the test class.....
Thanks a lot

TOP

原帖由 luckiejacky 於 2009-4-8 11:28 發表


Hello Panda Ching,
Sorry I did not mention, that is another program... I have the test class.....
Thanks a lot


class MUST start with capital letter....
JAVA is case sensitive....

frankly, I think you'd better read your books/notes carefully before asking these simple questions...

[ 本帖最後由 etkin 於 2009-4-8 16:33 編輯 ]

TOP

回覆 4# 的帖子

呢個係好常見嘅新手error - 你自己o向#3度都講左個Console object係null, 咁用一個null嘅object去invoke一個method, 當然會出NullPointerException啦

你要修改program嘅logic, Console object係null時, 就唔call printf()

至於#6, 你應該係無set PATH同CLASSPATH, 亦有可能係你用java test.java (而唔係java test)黎run個program

TOP