Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 7035

Running Java code in Ubuntu 22.04 distributive

$
0
0

I'm new in this field, and trying to do some tests using java in Ubuntu

I wrote a code in Java to connect my localhost mssql server and create a new database.the code is tested on windows and it's working expectedly, i copied pasted in Ubuntu server 22.04 server once i run the following command "java db_connect.java" I'm getting the following error

"Error: Could not find or load main class db_connectCaused by: java.lang.NoClassDefFoundError: db_connection/db_connect (wrong name: db_connect)"

my main public class name is db_connect.

what should I change in the code or add modules to try to run the compiled java file.

in advance thank you!

running the following command and getting an error.command: java db_connect.javaoutput: Error: Could not find or load main class db_connectCaused by: java.lang.NoClassDefFoundError: db_connection/db_connect (wrong name: db_connect)

find the code below:

package db_connection;import java.sql.*;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class db_connect {  void func1() {    String url =("jdbc:sqlserver://localhost:1433;integratedSecurity=true;");    String user="";          String password = "";    try(Connection conn = DriverManager.getConnection(url,user,password)) {                   Statement stmt = conn.createStatement();              String sql1 = "create database TM_users";                               stmt.executeUpdate(sql1);      System.out.println("Database created successfully...");  //check if DB Exists      String checkDBName = "bd1";      ResultSet rs = conn.getMetaData().getCatalogs();      boolean dbExists = false;      while (rs.next()) {        String databaseName = rs.getString(1);        if (databaseName.equalsIgnoreCase(checkDBName)) {          dbExists = true;          break;        }      }      rs.close();      if (dbExists) {        System.out.println("Database already exists.");      }else {        String sql = "CREATE DATABASE db_1";        stmt.executeUpdate(sql);        System.out.println("Database 'db_1' created successfully.");      }    }    catch(Exception e) {      System.out.println("Err =>" + e.getMessage());           }  }  public static void main(String[] args) throws SQLException {    db_connect ob1 = new db_connect();    ob1.func1();  }}

Viewing all articles
Browse latest Browse all 7035

Latest Images

Trending Articles



Latest Images