Many people having doubts/issues regarding including the external jar file while executing the JAVA program through the command line or using BAT file.
In this article I will just go thorugh the steps of "Running JAVA program using command line OR BAT file and specifying the external jars"
Let suppose:
1. I have a JAVA file named "DBAdapter.java" assuming it doesn't have the package directive on top of this JAVA file.
2. This java file is using the external jar named "mysql-connector-java-3.1.12-bin.jar".
3. Both JAVA and JAR file present into the folder named "DB".
How to compile:
Open the command prompt and cd to the "DB" directory where the JAVA and jar files present and execute below code:
javac -cp mysql-connector-java-3.1.12-bin.jar DBAdapter.java
This will genrate the "DBAdapter.class" file under the "DB" folder.
How to RUN:
Open the command prompt and cd to the "DB" directory where the JAVA and jar files present and execute below code:
java -cp "mysql-connector-java-3.1.12-bin.jar;." DBAdapter
Note: Importemt thing is adding a dot after the last samicoln while including the JARS. Dot will tell the java to look for class file in same folder.
You can see below the command line images for more details:
Let suppose if you are keeping the "DBAdapter.class" file in DB2 folder which is present into the DB folder in that case:
Code will be like this:
java -cp "mysql-connector-java-3.1.12-bin.jar;DB2" DBAdapter
If "DBAdapter.java" having the package directive like:
package com.DBAdaptor
In this case above command line code change to
java -cp "mysql-connector-java-3.1.12-bin.jar;." DBAdapter
Please let me know if you have any doubts?
In this article I will just go thorugh the steps of "Running JAVA program using command line OR BAT file and specifying the external jars"
Let suppose:
1. I have a JAVA file named "DBAdapter.java" assuming it doesn't have the package directive on top of this JAVA file.
2. This java file is using the external jar named "mysql-connector-java-3.1.12-bin.jar".
3. Both JAVA and JAR file present into the folder named "DB".
How to compile:
Open the command prompt and cd to the "DB" directory where the JAVA and jar files present and execute below code:
javac -cp mysql-connector-java-3.1.12-bin.jar DBAdapter.java
This will genrate the "DBAdapter.class" file under the "DB" folder.
How to RUN:
Open the command prompt and cd to the "DB" directory where the JAVA and jar files present and execute below code:
java -cp "mysql-connector-java-3.1.12-bin.jar;." DBAdapter
Note: Importemt thing is adding a dot after the last samicoln while including the JARS. Dot will tell the java to look for class file in same folder.
You can see below the command line images for more details:
Let suppose if you are keeping the "DBAdapter.class" file in DB2 folder which is present into the DB folder in that case:
Code will be like this:
java -cp "mysql-connector-java-3.1.12-bin.jar;DB2" DBAdapter
If "DBAdapter.java" having the package directive like:
package com.DBAdaptor
In this case above command line code change to
java -cp "mysql-connector-java-3.1.12-bin.jar;." DBAdapter
Please let me know if you have any doubts?
No comments:
Post a Comment