SpeedEX505
SpeedEX505

Reputation: 2076

JDBC java compilation running

I have server which communicates with database via 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:

java -cp jdbc.jar package.Server I get:

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

what is the right way?

Upvotes: 0

Views: 61

Answers (3)

grasskode
grasskode

Reputation: 158

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).

You can now run your code from the project root by adding classes and lib folders to your classpath :

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.

Upvotes: 1

Rajesh
Rajesh

Reputation: 556

May be you are using 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. :)

Upvotes: 0

Reimeus
Reimeus

Reputation: 159784

Add the current directory to the classpath

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

Upvotes: 3

Related Questions