在终端中运行Java程序。提出ClassNotFoundException

时间:2016-06-16 16:35:46

标签: java intellij-idea terminal compiler-errors

我用java创建了一个shell程序。我正试图通过终端运行该程序。但它继续提高ClassNotFoundException。该程序在IntelliJ上运行完全正常,但在终端上运行会导致异常。

这是我运行的终端脚本:

javac -d bin/ driver/Shell.java
java -cp bin/ driver.Shell

现在我在这里获得class.forName和all: 注意:classes变量是Hashtable,将名称映射到路径。

    String[] cmd = input.split(" ");

    String commad = classes.get(cmd[0]);
    try{
        Class<?> foundedCommand = java.lang.Class.forName(commad);
        Constructor cons = foundedCommand.getConstructor(String.class);
        Command runCmd = (Command) cons.newInstance(cmd[1]);

在Itellij上运行此功能非常好。但在终端中它是ClassNotFoundException。请帮忙

这是Runner.java类,由Shell.java

调用
package runner;


import commands.Command;
import exceptions.DirectoryExistsException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import java.util.Hashtable;

public class Runner {

    private String input;

    private Hashtable<String, String> classes = new Hashtable<String, String>
            (){
        {
            put("mkdir", "commands.MakeDirectory");
        }
    };

    public Runner(){}

    public Runner(String input){
        this.input = input;
    }

    public String[] runCommand(){
        String[] cmd = input.split(" ");

        String commad = classes.get(cmd[0]);
        try{
            Class<?> foundedCommand = java.lang.Class.forName(commad);
            Constructor cons = foundedCommand.getConstructor(String.class);
            Command runCmd = (Command) cons.newInstance(cmd[1]);
            return runCmd.run();

        }
        catch (DirectoryExistsException e){
            return new String[]{"", e.getMessage()};
        }

        catch (ClassNotFoundException e){
            return new String[]{"", e.getMessase()};
        }
        catch (NoSuchMethodException e){
            return new String[]{"", e.getMessase()};
        }
        catch (InstantiationException e){
            return new String[]{"", e.getMessase()};
        }
        catch (IllegalAccessException e){
            return new String[]{"", e.getMessase()};}
        catch (InvocationTargetException e){
            return new String[]{"", e.getMessase()};}

    }
}

这是Shell.java类:

package driver;


import logger.ConsoleLog;
import runner.Runner;

import java.util.Scanner;

public class Shell {

    public void runShell(){
        ConsoleLog log = new ConsoleLog();
        Scanner input = new Scanner(System.in);
        log.printPromptMessage();
        String uI = input.nextLine();

        while(!uI.equals("exit")){

            Runner run = new Runner(uI);
            String[] output = run.runCommand();

            if(!output[0].equals("")){
                log.setPromptMessage(output[0]);
            }
            log.printOutput(output[1]);
            log.printPromptMessage();
            uI = input.nextLine();
        }
        System.out.println("exiting Shell");

    }

    public static void main(String[] args) {
        Shell shell = new Shell();
        shell.runShell();
    }

}

ConsoleLog基本上是System.out.print()的东西。 这是终端中的错误:

/# mkdir /Users/KevinMacbook/Documents/CSC207/exercises/test
commands.MakeDirectory

这是跟踪:

java.lang.ClassNotFoundException: commands.MakeDirectory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at runner.Runner.runCommand(Runner.java:35)
    at driver.Shell.runShell(Shell.java:20)
    at driver.Shell.main(Shell.java:35)

0 个答案:

没有答案