Board logo

標題: Simple Java program [打印本頁]

作者: luckiejacky    時間: 2009-4-1 17:53     標題: 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 編輯 ]
作者: thinkpanda    時間: 2009-4-1 18:08

原帖由 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 ...



唔明你句英文寫乜野....
作者: luckiejacky    時間: 2009-4-1 18:12

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 編輯 ]
作者: luckiejacky    時間: 2009-4-1 21:09

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
作者: hdvd-rom    時間: 2009-4-1 22:00

please read java se api the Console section(java.io.Console), then you will know why.
作者: luckiejacky    時間: 2009-4-7 21:12

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 編輯 ]
作者: thinkpanda    時間: 2009-4-7 23:37

原帖由 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.
作者: luckiejacky    時間: 2009-4-8 11:28

原帖由 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
作者: etkin    時間: 2009-4-8 16:32

原帖由 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 編輯 ]
作者: 李且麒    時間: 2009-4-8 23:52     標題: 回覆 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
作者: patrickit    時間: 2009-4-9 08:19

呢句 :
if (console == null) { ... }
PHP 寫法... 我估你一定係寫開php啦....
作者: 十三    時間: 2009-4-9 10:20

提示: 作者被禁止或刪除 內容自動屏蔽
作者: luckiejacky    時間: 2009-4-9 16:27

Thanks all ching
I've checked everything
In summary,
Under Eclipse, it shows on the console window "Sales: unable to get console"
But under dos prompt, the app shows "string"...
So where should be the problem?
I am looking for some articles/documents for newbie in eclipse
Anyone knows? And when I delete the variable CLASSPATH, the app runs fine under dos prompt.
If I have CLASSPATH, stack traces occur...
Thanks
作者: t61    時間: 2009-4-9 16:39

老友, 你用 Eclipse 去 run 呢個program, Eclipse 係調用  JDK 下面既 javaw.exe
javaw.exe 是沒有 console 這個東西的, javaw.exe 使用來跑 Swing, AWT呢D野既.

你係  dos prompt 下面係用正常的 java.exe, 當然正常啦.

初初學 Java 都係唔好用IDE...   你俾呢個無謂既問題困擾佐甘耐...
學從難處學, 用從易中用.

原帖由 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 ...

[ 本帖最後由 t61 於 2009-4-9 16:43 編輯 ]
作者: luckiejacky    時間: 2009-4-9 16:58

原帖由 t61 於 2009-4-9 16:39 發表
老友, 你用 Eclipse 去 run 呢個program, Eclipse 係調用  JDK 下面既 javaw.exe
javaw.exe 是沒有 console 這個東西的, javaw.exe 使用來跑 Swing, AWT呢D野既.

你係  dos prompt 下面係用正常的 java.exe, 當然正 ...


Ok... Thanks, I understand now.... Thanks all ching





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2