1

I have written a java program using Blue J as environment. Now this program uses some external libraries (jar files), for example the jar file for the J Connector to connect to MySQL database and also 2 other jar files. In Blue J I simply added the jar files to my project and when I start the program in Blue J, everything works fine. But now I would like to create a .bat file to make it possible to run the program on other computers. I know that normally I start the compiled program with a batch file simply by writing into the batch file

java myApplication

But how can I "tell" the batch file that it should also use the jar files mentioned above? When I use just the code above the bat file doesn't see them and I get the exception "No suitable driver found for jdbc:mysql ... ", which obviously means, he has problems with finding the jar file (because, as I said, starting the program in Blue J works fine). I'm quite new to creating batch files. Can you please tell me how I can get this working?

Thanks and greetings, Daniel

1 Answer 1

2

You just need to set the classpath:

java -cp jarfile1.jar;jarfile2.jar;jarfile3.jar;yourmainjar.jar MainClass

In -cp you list all the JARs needed to run your application (including your main application JAR). On Windows, you separate them with semicolon ;, on Unix-like systems the separator is colon :.

If you use Maven to build your program, you could assemble a fat JAR: that is a JAR that, along with your application classes, contains all the classes from the JARs it uses. Maven Assembly plugin may be used to build such a JAR: http://maven.apache.org/plugins/maven-assembly-plugin/ For example, its jar-with-dependencies http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies may be useful.

To launch your program from a fat JAR you just do

java -jar the-fat-jar.jar
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, it works now! :) Also I just noticed, that in Blue J I have the option to export the project as jar file with all the external libraries already included - which makes thins even easier :)
@DanielBK Doing that is not always a good idea. For example if you need multiple JDBC drivers, the service definition files are usually not correctly merged (if merged at all), and then suddenly some drivers are not automatically loaded any more; same goes for other libraries that rely on service definitions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.