从 java 代码调用 rabbitmq 发布者

Invoking rabbitmq publisher from java code

我有 2 个基本 programs.first,一个叫做 ext.java,第二个叫做 send.java。要发布的内容位于一个名为 data[= 的文件中32=]

我使用 apache commons 库函数 FileUtils.readFileToString(file,format) 读取文件。
当我从命令行执行 send.java qname filepath 时,它会推送内容。
但是当我从 ext.java 程序调用相同的程序时,传递命令行参数(在 eclipse 项目中),它会抛出如下异常:

java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

环境变量都设置好了properly.Here是代码文件

ext.java

public static void main(String[] args) throws IOException {     
        File f = new File("C:\Users\Pradeep\Desktop\pros\externalJava\data");                 
        Runtime.getRuntime().exec("cmd.exe /c start java send f0 "+f.getAbsolutePath());
    }

send.java

import com.rabbitmq.client.*;
import org.apache.commons.io.*;
import java.io.File;

public class send {

  public static void main(String[] argv) throws Exception {
     ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel(); 
    File f = new File(argv[1]);
    String message = FileUtils.readFileToString(f,"UTF-8");
    channel.basicPublish("", argv[0], null, message.getBytes("UTF-8"));

    channel.close();
    connection.close(); 
  }
}

您还需要指定 classpath,以便将 JVM 指向查找 FileUtils 的位置(以及与 rabbitmq 相关的 类)。这里有相关文档 https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

另一种方法是创建可执行文件 JAR,这样类路径将包含在 JAR 的清单文件中,从而无需在 CLI 上指定类路径。它会是 运行 就像 java -jar Executable.Jar

所以试试这个:

 Runtime.getRuntime().exec("cmd.exe /c start java -cp PathToYourLibraries send f0 "+f.getAbsolutePath());

它从 eclipse 开始工作,因为它为用户设置了正确的类路径 t运行 - 将 libraries 中的所有条目添加到类路径以及编译的路径 类.