使用 -cp 选项执行时无法加载类 [英] Could not load class when executed with -cp option

查看:15
本文介绍了使用 -cp 选项执行时无法加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 在使用 -cp 选项执行时无法找到类文件,如下所示

Java not able to find the class file when executed with -cp option as below

javac -cp ~/softwares/pig-0.12.0/pig-0.12.0.jar PR.java

编译成功.但是,当我运行上面生成的类时,出现错误

Compilation is successful. However when I run the above generated class I am getting error

java -cp ~/softwares/pig-0.12.0/pig-0.12.0.jar PR
 Error: Could not find or load main class PR

如果我删除 -cp 我会得到低于预期的错误

If I remove the -cp I am getting below error which is expected

 java PR
 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pig/PigServer
    at PR.runPigScript(PR.java:9)
    at PR.main(PR.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.pig.PigServer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 2 more

你能告诉我失败的原因是 Step-2(Could not find or load main class) .下面是PR.java的代码

Could you please let me what might be reasons for failure is Step-2(Could not find or load main class) . Below is the code of PR.java

  import org.apache.pig.ExecType;
  import org.apache.pig.PigServer;
  import org.apache.pig.backend.executionengine.ExecException;

 public class PR {

    public void runPigScript(){
            try {
                    PigServer  pigServer = new PigServer(ExecType.LOCAL);
                    pigServer.registerScript("RP.pig");
            } catch (Exception ex) {
                    // TODO Auto-generated catch block
                    ex.printStackTrace();
            }
    }

  public static void main(String[] args){
      PR pr = new PR();
     pr.runPigScript();
  }

}

来自 https://wiki.apache.org/pig/EmbeddedPig

要运行您的程序,您需要先使用以下命令编译它:

To run your program, you need to first compile it by using the following command:

   javac -cp <path>pig.jar WordCount.java

如果编译成功,就可以运行你的程序了:

If the compilation is successful, you can then run your program:

   java -cp <path>pig.jar WordCount

推荐答案

尝试使用:

java -cp ~/softwares/pig-0.12.0/pig-0.12.0.jar;. PR

问题是您还需要加载已编译的 PR 类.因此,您的类路径需要同时具有依赖项和编译后的输出.将当前目录添加到类路径.

The problem is that you need to load your compiled PR class as well. So your classpath needs to have both dependencies and your compiled output. Add current directory to the classpath.

这篇关于使用 -cp 选项执行时无法加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆