This error appears when trying to connect. But it appears if you run the jar file not from the project folder. In the project folder works as it should. It is used by NetBeans, and if correctly understood, then it is not necessary to touch the classpath variable. Just add a driver to the project. In general, how to run jar not only from the project folder?

Class.forName("com.mysql.jdbc.Driver") used.

  • The file with the driver is next to the file that you run? - MrFylypenko
  • No, the file is in the \ NetBeans \ modules \ ext \ mysql-connector.jar folder - Kamenev_D
  • But in the dist \ lib project folder there is a mysql-connector-java-5.1.23-bin.jar. And the Class-Path path is specified in the manifest file jar: lib / mysql-connector-java-5.1.23-bin.jar And how to be in this situation? Do you always carry this file with you? - Kamenev_D
  • It turns out that if you create a separate lib folder and copy where mysql-connector-java-5.1.23-bin.jar, then jar works independently of the folder. But this is not correct. How to do something mysql-connector-java-5.1.23-bin.jar was not required as a separate folder with the file. - Kamenev_D
  • If mysql-connector-java-5.1.23-bin.jar is added to the inside of an executable jar file, and Class-Path is specified, then everything should work, and just one file. In NetBeans, when generating a jar file, you only need to specify everything "creating a file with dependencies". - MrFylypenko

3 answers 3

Netbeans builds projects using Apache Ant. Netbeans by default creates a lib folder where it copies all the libraries necessary for the work of your project. This is for your convenience, so that you no longer have to write a long CLASSPATH upon further launching your project from the command line. All the necessary settings on this account are in the build.xml file in your project folder. You can make the necessary adjustments to this file at your discretion. You can also use another tool for building a project, for example, Maven or the JDK compiler in general. And yes, if your project requires any library, you will have to “drag” it with you. I would not recommend "embedding" the library inside your jar, besides, it is not always permissible by a license agreement to the library.

    Try to open the zip file with the executable jar file, it should have the following structure:

     jarname.jar | /META-INF | | MANIFEST.MF | | Main-Class: com.mydomain.mypackage.Main | | Class-Path: mysql-connector-java-5.1.23-bin.jar | /com/mydomain/mypackage | | Main.class | mysql-connector-java-5.1.23-bin.jar 

    those. in Class-Path, the relative path to the file must be specified and everything should work with a single executable file. See also this answer.

    • Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.4 Created-By: 1.8.0_66-b18 (Oracle Corporation) Class-Path: lib / mysql-connector-java-5.1.23-bin.jar X-COMMENT: Main-Class: condb.Condb - Kamenev_D
    • Ie there is a link to the lib folder. Class-Path: lib / mysql-connector-java-5.1.23-bin.jar - Kamenev_D
    • @Igor Kudryashov, thanks for the detailed answer, but still. How to embed a third-party jar in your own? - Kamenev_D
    • The link goes to the lib folder, and in this folder itself is mysql-connector-java-5.1.23-bin.jar, in the executable jar? - MrFylypenko
    • There is no this folder in the executable jar. How to embed it there is unknown. This is exactly the problem. - Kamenev_D

    The solution to the problem found here: http://www.oracle.com/technetwork/java/javase/overview/single-jar-141905.html

    In short: The build.xml file is changed, the following code is added at the end of the file before the closing "/ project>" tag:

      <target name="package-for-store" depends="jar"> <!-- Change the value of this property to be the name of your JAR, minus the .jar extension. It should not have spaces. <property name="store.jar.name" value="MyJarName"/> --> <property name="store.jar.name" value="MyJarName"/> <!-- don't edit below this line --> <property name="store.dir" value="store"/> <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/> <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/> <delete dir="${store.dir}"/> <mkdir dir="${store.dir}"/> <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip"> <zipgroupfileset dir="dist" includes="*.jar"/> <zipgroupfileset dir="dist/lib" includes="*.jar"/> <manifest> <attribute name="Main-Class" value="${main.class}"/> </manifest> </jar> <zip destfile="${store.jar}"> <zipfileset src="${store.dir}/temp_final.jar" excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/> </zip> <delete file="${store.dir}/temp_final.jar"/> </target> 

    In line

     <property name="store.jar.name" value="MyJarName"/> 

    The value value is replaced with the name of your project.

    Then on the "Files" tab, select your project-> build.xml-> right-click-> Run target-> Other goals-> package-for-store. As a result, a folder "folder" is created in the project folder containing the packed result file.

    Thank you all for your participation and help.