2

I have gone through all the suggested questions, still no solution.

I am running a java program of jdbc connection on eclipse, its giving the required result but when making JAR of the same code and running on eclipse I am getting error :

Command prompt error

I have already added jar file for mysql connection:mysql-connector-java-5.1.38-bin.jar as an external jar to the project.

My source code :

    package com.ari;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Test {
    static String dbPath="jdbc:mysql://172.19.24.66:3306/chatdb";
    static String dbUser="root";
    static String dbPass="root";
    static String name="";
    public static Connection con;
    static PreparedStatement ps=null;
    ResultSet rs=null;
    static String query="";

    public static void main(String arg[]) throws ClassNotFoundException, SQLException
    {

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

        con=DriverManager.getConnection(dbPath,dbUser,dbPass);
        query="Insert into user(username) values(?)";
        ps=con.prepareStatement(query); // error at this line
        Scanner s=new Scanner(System.in);
        name=s.next();
        ps.setString(1, name);




        if(ps.executeUpdate()!=0)



    System.out.println("Welcome"+ name+" to chat room.");


    }

}

My manifest file :

Manifest-Version: 1.0
Main-Class: com.ari.Test
dexter
  • 275
  • 1
  • 3
  • 16
  • It is not able to find the mysql connector jar. – Bilesh Ganguly May 16 '16 at 09:36
  • It means you didn't package the MySQL JAR with your executable JAR properly. – duffymo May 16 '16 at 09:38
  • Take a look at http://stackoverflow.com/questions/25186557/how-to-compile-mysql-jdbc-driver-with-a-java-file-in-cmd – Bilesh Ganguly May 16 '16 at 09:38
  • @duffymo how to package MySQL jar with my JAR ? – dexter May 16 '16 at 09:40
  • Easy to do with Maven: https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – duffymo May 16 '16 at 09:41
  • and what about without maven? is there no way to do so? – dexter May 16 '16 at 09:43
  • Can you please post your manifest file which is contained in the jar? – Joao Esperancinha May 16 '16 at 09:43
  • @dexter you can use several options in Maven. You can use the [shade plugin](https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html), [one jar](http://one-jar.sourceforge.net/index.php?page=documents&file=whatsnew) and the [assembly-plugin](https://maven.apache.org/plugins/maven-assembly-plugin/usage.html). My preferred is definitelly shade. – Joao Esperancinha May 16 '16 at 09:46

3 Answers3

0

While Compiling use the following ->

javac YourJavaFile.java // This will compile your java file.

java -cp .;completePathOfMysqlConnector/mysql-connector-java-5.1.38-bin.jar YourJavaFile  //Copy the mysql jar to the class path for the YourJavaFile.class file.

Try this and let me know if this works..

Ajinkya Patil
  • 741
  • 1
  • 6
  • 17
0

You have to specify classpath information while running the command or else you need to add classpath information to your jar manifest file. Please see https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

sch
  • 21
  • 5
0

That is solved now, actually I was making "JAR file" for exporting my project, in place of that I have to select "Runnable JAR file".

enter image description here

dexter
  • 275
  • 1
  • 3
  • 16