简体   繁体   English

在Java类中加载jar

[英]Load a jar in Java Class

I have a simple java class I want to load a jar file in my main method. 我有一个简单的Java类,想在我的main方法中加载jar文件。 How can I load the jar so that its available 我如何装载罐子以便可用

  public static void main(String args[]) throws SQLException, IOException{

    String connectionString = "connectionstring";
    File platformDB = new File(tempDir, "PlatformDB.eap");

    createNewTempDirectory();
    Mirror  m = new Mirror(connectionString, createEmptyEap(emptyEapName) ,platformDB.getAbsolutePath());
    m.run();
}

Mirror Class 镜类

    static {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
        }
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
        }
}

In Mirror.run method I try to make a connection to database 在Mirror.run方法中,我尝试建立与数据库的连接

Mirror.run() method Mirror.run()方法

 public void run() {
    this.source = DriverManager.getConnection(EaDbStringParser.eaDbStringToJdbc(sourceString));
    this.source.setReadOnly(true);
}

I have a jar file located under "C:\\sqljdbc_4.0\\enu\\sqljdbc4.jar" How can I load this jar so the connection is made successfully 我在“ C:\\ sqljdbc_4.0 \\ enu \\ sqljdbc4.jar”下有一个jar文件。如何加载此jar,以便成功建立连接

Thanks 谢谢

您只需要使用-classpath选项运行该类:

java -classpath C:\sqljdbc_4.0\enu\sqljdbc4.jar

The simplest way is to add the jar to the classpath loading the main class. 最简单的方法是将jar添加到加载主类的类路径中。 For example if Mirror is the class with the main method where you say from console 例如,如果Mirror是具有main方法的类,那么您可以在控制台中说

`java Mirror' 

modify it to 修改为

'java -cp C:/sqljdbc_4.0/enu/sqljdbc4.jar Mirror`

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

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