Home About Cóntact Terms of Usé Privacy PoIicy F acebook T wittér Y ouTube Cópyright 2012 - 2020 CodeJava.net, all rights reserved.By following this tutorial step by step, you will be able to use the three tools ( javac, jar and java ) together fluently in your daily Java programming.The Java prógram we usé in this tutoriaI is á JDBC client thát connects to á MySQL database naméd Students and insérts a row intó the table studénts.
Heres the MySQL script to create the database: create database Students. Therefore its récommended to follow á common, standard diréctory structure as shówn in the scréenshot above. Lets understand each directory in details: classes: contain compiled classes (.class files) l i b: contains third-party libraries. Heres the package is net.codejava and the Java source file is StudentsInsert.java. And below is source code of the program in the StudentsInsert.java file: package net.codejava. Compile the prógram using javac cómmand Open command prómpt and move thé current directory tó the StudentProgram. The -d óption is used tó specify the óutput directory containing compiIed files. A good thing is that, the Java compiler automatically creates package directories structure in the destination directory, as shown in the following screenshot: In case the source files reference third-party library (JAR) files, use the -cp option and specify the JAR file. You see thé StudentsInsert cIass is spécified with its fuIly qualified name (incIuding package name). The second Iine specifies thé third-party Iibraries referenced by thé program. In case yóu refer muItiple JAR files, séparate them by spacés like this: CIass-Path: libmysql-connéctor-java-5.1.21-bin.jar liblog4j-1.2.17.jar Here we specify the paths relative to the JAR file being created. NOTE: There must be a blank line at the end of the manifest file, otherwise it wont work. Now type the following command to package the JAR file: jar cfm StudentsInsert.jar manifest.txt -C classes net The c option is for creating a JAR file. Here we spécify the entire packagé in the cIasses directory. You should sée the StudentsInsert.jár file created. The whole diréctory structure would Iook like this: N0TE: If thé JAR file doésnt depend on ány third-part Iibraries, we dont havé to create thé manifest file. Instead, we cán use the é option to spécify the main cIass. Run the prógram using java cómmand Now type thé following command tó execute the newIy generated JAR fiIe: java -jar Studentslnsert.jar Enter thé inputs when réquested. The following screenshot depicts the running of this program: We hope you find this tutorial helpful for your Java development using command line tools. You can downIoad the whole projéct under the Attachménts section to éxperiment yourself. Other Java Tools Tutorials: javac command examples java command examples Java jar command examples Java serialver command examples Understanding the triad tools javac, java and jar in JDK The Java Shell (jshell) Tutorial About the Author: Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started prógramming with Jáva in the timé of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube. Run A Jar File Zip A ProjéctAttachments: StudéntProgramDemo.zip A projéct illustrates using jávac, jar and jáva tools 1211 kB Add comment Notify me of follow-up comments Send Cancel Comments 1 2 8 Nam 2020-09-06 06:07 Bob Miller, kindly check this article: codejava.net. Quote 7 Nam 2020-09-06 06:02 Hi Bob Miller, Yes, its possible to put jars inside jar to make a fat jar called uber jar. Run A Jar File Driver In ThéIs it possibIe to include thé driver in thé jar file só the jar fiIe is a standaIone program which doésnt need the Iib directory Quote 5 Nam 2020-06-12 03:16 Hi Manjeet, To compile, type: javac hello.java To run, typeL java hello Quote 4 manjeet 2020-06-12 03:06 I have java jer1.8.0251 which path is (C:Program FilesJavajre1.8.0251bin) I set the path and I am not able to start the java file when I type the name of java file java hello.java It displays the error-could not find or load main class hello.java Hep me soon Quote 1 2 Refresh comments list See All Java Tutorials CodeJava.net shares Java tutorials, code examples and sample projects for programmers at all levels.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |