Sar009
Sar009

Reputation: 2276

java programs not running due to seting classpath

i wanted to practice developing database programs in java so i set the classpath to E:\software\installed\java\jre\lib by declaring a new environmental variable classpath and gave value as E:\software\installed\java\jre\lib but now i am unable to run any program(not even non-database) i get an error

Error: Could not find or load main class MysqlConnect

but when i delete the classpath variable i am able to run non-database programs. what is the possible problem and please let me know the solution. i have set path to E:\software\installed\java\bin

nothing is helping i tried using class path switch in java which is not helping (java -cp C:\Program Files\MySQL\MySQL Server 5.5 MysqlConnect) nor do setting path is helping (set CLASSPATH=%CLASSPATH%:C:\Program Files\MySQL\MySQL Server 5.5:C:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar) all are giving the error

Error: Could not find or load main class MysqlConnect

Upvotes: 1

Views: 3556

Answers (5)

Sar009
Sar009

Reputation: 2276

ok atlast i got the solution it should be typed as

C:\Users\sarad mohanan\Desktop\rose>java -cp .;"c:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar" MysqlConnect MySQL Connect Example. Connected to the database Disconnected from database

we have to add .; before the original path . holds default classpath. pravel veller had said it previously but i didn't understand it then

Upvotes: 1

user1272893
user1272893

Reputation: 11

Here's a jdbc tutorial for beginners that could help you find your answer. If you skip to step 14 it gives an example of how the classpath is used. It is used as the first respondent is saying, by command line.

Hopefully this will help you in your particular situation. This tutorial is not database-specific so it will work with any type of database you are using.

Upvotes: 1

hekomobile
hekomobile

Reputation: 1388

Crete o set environment variable if you use windows JAVA_HOME=E:\software\installed\java, and after append the Java bin directory %JAVA_HOME%\bin. Note that paths are separated from each other with semicolons (;) I hope help you. That's work for me !!!. I don't understand. What happen?.

Upvotes: -1

The Original Android
The Original Android

Reputation: 6215

I would like to propose using a good tool like Eclipse. You can test the CLASSPATH indirectly and your environment by adding or removing jar files and libraries.

Obviously, the libraries in your default CLASSPATH is different than the one in your E drive. You may compare the files by knowing your CLASSPATH. In Windows, do "echo %CLASSPATH%".

Eclipse can help you by experimenting without rebooting or any annoying steps. And this stuff is not easy.

Upvotes: 0

Pavel Veller
Pavel Veller

Reputation: 6115

You shouldn't be defining a classpath for your programs using system variables. Standard way is to use the command line -cp or -classpath option. Take a look at what java prints out if you run it with no arguments.

You will find this mentioned in this tutorial. It says:

The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care

Also, see how the default CLASSPATH environmetn variable has a . in it. If you still decide to add your classes in there, keep the . for other citizens and add your classes after a ;, don't just overwrite the whole value.

Upvotes: 1

Related Questions