gpt4 book ai didi

java - 在终端编译maven netbeans项目

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:42 25 4
gpt4 key购买 nike

我想在我的 linux 终端中编译一个 Netbeans Maven - Java 应用程序。

这是因为我想使用 Git CVS 并能够在独立于 IDE 的服务器中编译项目。

但我很难真正理解依赖关系是如何工作的,或者这就是我的想法。

我正在使用一个简单的 java 应用程序代码连接到 mysql 数据库并获取一些信息。

我正在使用这段代码:

public static void main(String[] args) throws Exception {

Class.forName("com.mysql.jdbc.Driver");
try (Connection connect = DriverManager.getConnection("jdbc:mysql://192.168.2.21/altadb?" + "user=marco&password=Marco19")) {

Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery("select idCuentaConcentradora, noCliente, nombreCliente, noCuenta, nombreCuenta from altadb.catCuentaConcentradora");

while (resultSet.next()) {
int idCuentaConcentradora = resultSet.getInt("idCuentaConcentradora");
String noCliente = resultSet.getString("noCliente");
String nombreCliente = resultSet.getString("nombreCliente");
String noCuenta = resultSet.getString("noCuenta");
String nombreCuenta = resultSet.getString("nombreCuenta");
System.out.println("idCuentaConcentradora: " + idCuentaConcentradora);
System.out.println("noCliente: " + noCliente);
System.out.println("nombreCliente: " + nombreCliente);
System.out.println("noCuenta: " + noCuenta);
System.out.println("nombreCuenta: " + nombreCuenta);
System.out.println("---------------------------------------------------");

}
} catch (SQLException e) {
System.out.println(e.getMessage());
}

}

然后将mysql-connector-java 添加到项目依赖项中,pom.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alta.MySQLAccess_Mvn</groupId>
<artifactId>MySQLAccess_Mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>

如果我将 netbeans IDE 与 clean 和 build 一起使用,然后运行项目,则没有错误,并且信息是从数据库中获取的。

如果我尝试从终端做同样的事情,我会得到一个错误:

[marko@mark-laptop MySQLAccess_Mvn]$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building MySQLAccess_Mvn 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MySQLAccess_Mvn ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MySQLAccess_Mvn ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ MySQLAccess_Mvn ---
[INFO] Compiling 1 source file to /home/marko/NetBeansProjects/MySQLAccess_Mvn/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MySQLAccess_Mvn ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/marko/NetBeansProjects/MySQLAccess_Mvn/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ MySQLAccess_Mvn ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MySQLAccess_Mvn ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MySQLAccess_Mvn ---
[INFO] Building jar: /home/marko/NetBeansProjects/MySQLAccess_Mvn/target/MySQLAccess_Mvn-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ MySQLAccess_Mvn ---
[INFO] Installing /home/marko/NetBeansProjects/MySQLAccess_Mvn/target/MySQLAccess_Mvn-1.0-SNAPSHOT.jar to /home/marko/.m2/repository/com/alta/MySQLAccess_Mvn/MySQLAccess_Mvn/1.0-SNAPSHOT/MySQLAccess_Mvn-1.0-SNAPSHOT.jar
[INFO] Installing /home/marko/NetBeansProjects/MySQLAccess_Mvn/pom.xml to /home/marko/.m2/repository/com/alta/MySQLAccess_Mvn/MySQLAccess_Mvn/1.0-SNAPSHOT/MySQLAccess_Mvn-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.866 s
[INFO] Finished at: 2014-06-18T18:46:28-06:00
[INFO] Final Memory: 12M/144M
[INFO] ------------------------------------------------------------------------

[marko@mark-laptop MySQLAccess_Mvn]$ java -cp target/MySQLAccess_Mvn-1.0-SNAPSHOT.jar com.marco.mysql.first.test.test
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.marco.mysql.first.test.test.main(test.java:23)

认为既然在 pom.xml 中声明了依赖项,mysql 驱动程序就会被添加到项目库中……但事实并非如此。

我想从终端,从构建到运行,从 Netbeans 获得相同的输出。

我不知道怎么做。

最佳答案

如果您已将 maven 添加到系统路径,那么您可以在提示符下使用 maven 命令简单地编译它:

mvm clean install

关于java - 在终端编译maven netbeans项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24296800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com