简体   繁体   English

使用-cp选项执行时无法加载类

[英]Could not load class when executed with -cp option

Java not able to find the class file when executed with -cp option as below 使用-cp选项执行时,Java无法找到类文件,如下所示

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

If I remove the -cp I am getting below error which is expected 如果我删除了-cp,我将得到以下预期的错误

 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

Could you please let me what might be reasons for failure is Step-2(Could not find or load main class) . 您能否让我失败的原因可能是步骤2(找不到或加载主类)。 Below is the code of PR.java 下面是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();
  }

}

From https://wiki.apache.org/pig/EmbeddedPig 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

Try to use: 尝试使用:

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

The problem is that you need to load your compiled PR class as well. 问题是您还需要加载已编译的PR类。 So your classpath needs to have both dependencies and your compiled output. 因此,您的类路径需要同时具有依赖项和已编译的输出。 Add current directory to the classpath. 将当前目录添加到类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM