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

查看:471
本文介绍了使用-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(无法找到或加载主类)。以下是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天全站免登陆