简体   繁体   English

JDBC Java编译运行

[英]JDBC java compilation running

I have server which communicates with database via JDBC. 我有通过JDBC与数据库通信的服务器。 When I compile it 当我编译它

javac -cp jdbc.jar package/Server.java

I get nonsense errors It can't find my classes in other files. 我会胡说八道错误在其他文件中找不到我的班级。 If I compile it without -cp option but run it with -cp option as: 如果我不使用-cp选项进行编译,但使用-cp选项将其运行为:

java -cp jdbc.jar package.Server I get: java -cp jdbc.jar package.Server我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: package/Server Caused by: java.lang.ClassNotFoundException: package.Server

what is the right way? 正确的方法是什么?

将当前目录添加到类路径

javac -cp .:jdbc.jar package/Server.java

Assuming that you have managed to compile your code and are having trouble executing it. 假设您已经设法编译了代码并且在执行代码时遇到了麻烦。 You need to have the following in your classpath : 您需要在类路径中包含以下内容:

  1. Compiled classes 编译类
  2. External dependencies 外部依赖

Let's say your project structure looks like follows : 假设您的项目结构如下所示:

|
|-- classes
  |-- package
    |-- Server.class
|-- lib
  |-- jdbc.jar

Here classes contain your compiled java classes (eclipse does it neatly but you might want to look into system specific builds if you are to run them on a different remote server) and lib contains all the external dependencies (jdbc.jar in your case). 这里的类包含编译的Java类(eclipse可以很好地完成它,但是如果要在不同的远程服务器上运行它们,则可能要研究系统特定的内部版本),而lib包含所有外部依赖项(在您的情况下为jdbc.jar)。

You can now run your code from the project root by adding classes and lib folders to your classpath : 现在,您可以通过将类和lib文件夹添加到类路径来从项目根目录运行代码:

java -cp ./classes:./lib package.Server

However, I strongly recommend using a standard project structure and looking into project managers like maven for maintaining projects and building across servers. 但是,我强烈建议您使用标准的项目结构,并咨询像maven这样的项目经理来维护项目和跨服务器构建。

May be you are using eclipse. 可能是您正在使用Eclipse。 And if is that so then just copy your code paste it in some newly created file in home directory and run it. 如果是这样,那么只需复制代码,将其粘贴到主目录中的某个新创建的文件中,然后运行它即可。

I was facing some problem and resolved it by doing above operation. 我遇到了一些问题,并通过执行上述操作解决了该问题。 :) :)

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

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