Add jar files to classpath at launch time - java

I want to add some jar files to my binary at runtime, but I keep getting errors. I'm running this on a Windows machine. My code is in a directory called SeleniumTest.
Here is the command I used to compile:
javac SeleniumTest\src\com\src\test\First.java -d SeleniumTest\bin -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar
This worked successfully. However when I try to run this command:
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin com.src.test.First
I get a message:
Error: Could not find or load main class SeleniumTest\bin
My code, First.java exists in
SeleniumTest\bin\com\src\test
What am I doing wrong?

try this
java -cp "SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar;SeleniumTest\bin" com.src.test.First

try following
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin SeleniumTest\src\com\src\test\First

Related

not able to add jar in linux server while executing java class

I am executing 2 java classes which have StringUtils function. For this I have common-lang3.jar so I am giving command
`javac -cp common-lang3.jar *.java`
this time no error but after that
java -cp common-lang3.jar abc.class its giving
Could not find or load main class For linux server where I have to place jar files so that i can just give command javac *.java ? If in classpath java -classpath is not allowing me to edit. pls help
Change
java -cp common-lang3.jar abc.class
to (assuming abc has your main method)
java -cp common-lang3.jar:. abc

"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver" error when running from terminal

I have a program that I run from Eclipse successfully.
However, when I want to run it from terminal, I encounter the famous error:
"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"
on this line:
Class drvClass = Class.forName("oracle.jdbc.driver.OracleDriver");
PS:
I have the following in CLASSPATH:
/oracle/jdbc/lib/ojdbc6.jar
Also note that I compile it successfully (javac Test2.java). Then when I run it (java Test2), I get the following error:
Error: Could not find or load main class Test2
So I run:
java -classpath ~/Desktop/JDBC2/src Test2
It runs, but I get the above "ClassNotFoundException" though.
I found this question tricky: the reason is related to semicolon after jar file address.
At first I changed the directory of MySample.java to another directory (you can don't do that) like C:\
then I removed package address from the source code, at the end I run this command in cmd
java -cp path_to_oracle_driver.jar; MySample
P.S. If you want run it from terminal you have to remove package PackageAddress from the source code and compile it again.
As #yngwietiger mentioned above in the comments, using -classpath parameter when running the .class file, overrides the original CLASSPATH and the predefined ojdbc6.jar file. So we need to mention both when running:
java -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar Test2
Or, as a better solution, we can add the current path to CLASSPATH (note the colon and dot at the end):
export CLASSPATH=$CLASSPATH:.
And, in order to run, we just need to type:
Java Test2

error: could not load or find main class xyz

I have checked my java installations by compiling and running a HelloWorld program which works perfectly fine.
The problem comes when I compile my program with certain jar files which are located in the same directory as my java file. This is what I've done.
javac -cp "A.jar:B.jar" MyProg.java
This generates the class file MyProg.class successfully. Next when I run the following command, it gives this error error: could not load or find main class MyProg
The command is:
java -cp "A.jar:B.jar" MyProg
Next, I even tried next by moving the jars in a folder named lib and issued the following commands:
javac -cp "lib/*" MyProg.jar (works fine;generates a class file)
java -cp "lib/*" MyProg (issues the same error)
I am working on a linux machine. Can some one please resolve the error.
Add the current path to the classpath
java -cp .:A.jar:B.jar MyProg

How to add external jar without Eclipse build path option?

My java file jdbc11.java was compiled successfully with javac jdbc11.java command in cmd, after that when I tried to to run java jdbc11 I got this exception:
java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
refering to this code in the file
Class.forName("com.mysql.jdbc.Driver");
, when I tried it in eclipse ,I added to "Java Build Path" the external jar : mysql-connector-java-5.1.20-bin.jar and it run successfully .
in case I didn't fix it with eclipse what should I done in the first try with java jdbc11 command in order to make work ?
note: the jar in the same dir with the jdbc11.java
Try adding the mysql-connector jar to the classpath when you execute your command-line code:
java -cp mysql-connector-java-5.1.20-bin.jar;. jdbc11
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html
You have to add -classpath in the execute command
java -classpath mysql-connector-java-5.1.20-bin.jar jdbc11
If I'm understanding you allright, what you are missing is a -cp option in your java command line, which is what eclispe would do internally if you were to put it in the build path.

Problems compiling and running Java app with Bluecove (NoClassDefFoundError)

I have this app that uses bluetooth, so I need both, bluecove and bluecove-gpl packages, when I run it in NetBeans I have no problem at all, and works perfectly fine. But I still can't compile and run from the command line (Ubuntu 11.04).
I'm using this line for compilation:
$ javac -Xlint:unchecked -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client.java
And it doesn't return errors and it generates a .class file
Then I try to run the .class file like this:
java -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
But it returns a NoClassDefFoundError.
Could not find the main class: SPPClient.
Why is this happening?
You probably need to add your current directory (or whatever directory your class files reside in) to the class path.
Try something like
java -classpath .:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
or
java -classpath bin:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
You need to have the main class definition in the manifest file:
Main-Class: classname

Categories

Resources