sjf_2293 2014-11-28 15:54 采纳率: 0%
浏览 1860

console类在IDE空指针在CMD下无法加载

package com.sun;

import java.io.Console;

public class ConsoleDemo {
public static void main(String[] args) {

  Console cnsl = null;
  String alpha = null;

  try{
     // creates a console object
     cnsl = System.console();

     // if console is not null
     if (cnsl != null) {

        // read line from the user input
        alpha = cnsl.readLine("Name: ");

        // prints
        System.out.println("Name is: " + alpha);

        // read password into the char array
        char[] pwd = cnsl.readPassword("Password: ");

        // prints
        System.out.println("Password is: "+pwd);
     }      
  }catch(Exception ex){

     // if any error occurs
     ex.printStackTrace();      
  }

}
}
在IDE中控制台没有显示,在CMD里运行找不到或无法加载主类,没办法使用啊求帮解答!!!多谢了

  • 写回答

1条回答

  • Coursera 2014-11-30 06:29
    关注

    在IDE中控制台没有显示是因为java虚拟机在试图生成一个native的console失败了,其实你得到了一个null。 CMD找不到活着无法加载类就是classpath的问题。你试试以下方法
    PATH=$JAVA_HOME/bin:$PATH
    javac ConsoleDemo.java
    java ConsoleDemo //如果不行,改成 java -cp ./ ConsoleDemo

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog