1

I'm using postgresql-42.2.23.jar in my following program.i have tested the code in windows environment in intelij ide and it is working fine.But i want to run it in the linux machine( which runs debian 10)using the comand line.Right now postgresql-42.2.23.jar file and java class files are in the Music folder.

when i compile the program with "chathu@giottestserver:~/Music$ javac -cp Music/postgresql-42.2.23.jar serverinsert.java" command,it compiles and create the class file.

But when i run it with "chathu@giottestserver:~/Music$ java -cp .:Music/postgresql-42.2.23.jar:serverinsert" it gives following messageenter image description here

I have tried with following command also.But no output. " chathu@giottestserver:~/Music$ java -cp .:Music/postgresql-42.2.23.jar serverinsert"

can anyone help me to solve this?

import java.sql.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class serverinsert {
    public static void main(String []V){
        Connection c = null;
        Statement stmt = null;
        try
        {
            //Connecting to the database in local machine

            Class.forName("org.postgresql.Driver");
            c = DriverManager
                    .getConnection("jdbc:postgresql://localhost/",
                            "postgres", "gflow123");
            System.out.println("Opened database successfully");

            //Creation of a table to insert data in the local machine

            stmt = c.createStatement();
            String sql = "CREATE TABLE jason " +
                    "(ID INT      NOT NULL," +
                    " sendata json NOT NULL)";
            stmt.executeUpdate(sql); //updates the table
            System.out.println("Table created successfully");

            //Opening connection for DLC client

            ServerSocket ss = new ServerSocket(4000);
            Socket s = ss.accept();
            StringBuilder sb = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line;

            // DataOutputStream Dout = new DataOutputStream(s.getOutputStream());
            // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            //String MsgIn="",MsgOut="";

            // inserting the DLC Data into a string variable called content
            String content="";
            while((line = br.readLine()) != null){
                sb.append(line).append(System.lineSeparator());
                content = sb.toString();

            }


            //Removing the http header content from the DLC Data

            String jsn=content.substring(content.indexOf("{"));

            //DLC received data processing
            System.out.println(jsn);
            String jsonreplacd=jsn.replace("[{\"name","\"name");
            System.out.println(jsonreplacd);
            String jsonreplacd1=jsonreplacd.replace("\"name","[\"INSERT INTO jason (ID,sendata) \" + \"VALUES ('1','')");
            System.out.println(jsonreplacd1);
            String jsonreplacd2=jsonreplacd1.replace("')\":\"press\",\"datatype\":\"float\",\"content\":","");
            System.out.println(jsonreplacd2);
            String jsn2=jsonreplacd2.substring(jsonreplacd2.indexOf("["));
            System.out.println(jsn2);
            String jsn3=jsn2.replace("[{","{");
            System.out.println(jsn3);
            String jsn4=jsn3.replace("\"t\"","\\\"t\\\"");
            System.out.println(jsn4);
            String jsn5=jsn4.replace("\"v\"","\\\"v\\\"");
            System.out.println(jsn5);
            String jsn6=jsn5.replace(":",":\\\"");
            System.out.println(jsn6);
            String jsn7=jsn6.replace(",\\","\\\",\\");
            System.out.println(jsn7);
            String jsn8=jsn7.replace("},","\\\"},");
            System.out.println(jsn8);
            String jsn9=jsn8.replace("}]}]}}","\\\"}')\"");
            System.out.println(jsn9);
            String jsn10=jsn9.replace("[","");
            System.out.println(jsn10);
            String jsn11=jsn10.replace("},{","}')\",\"INSERT INTO jason (ID,sendata) \" + \"VALUES ('1','{");
            System.out.println(jsn11);
            String jsn12=jsn11.replace("\",\"","\"@\"");
            System.out.println(jsn12);
           // String jsn3=jsn2.replace("\"","\\\"");
           //
           //
          //  String jsn6=jsn5.replace(",","\\\",");
          //  String jsn7=jsn6.
          //
           // String jsn9=jsn8.replace("}\\\"","}");

           // System.out.println(jsn9);

            // Creation of string array type to execute sql command

            //String[] strArray = new String[] {jsn11};
            //System.out.println(strArray[0]);

//String hi="hi";
            //String[] data = {"INSERT INTO jason (ID,sendata) " + "VALUES ('1','{\"t\":\"2104492460\",\"v\":\"-0.000769\"}')"};
            String[] data = jsn12.split("@");
            for(int i=0;i<5;i++) {
                //stmt.executeUpdate(data[i]);
                System.out.println(data[i]);
         }





            s.close();
        }

        catch(Exception e){

        }

    }
}

1 Answer 1

1

You are trying to run application with postgresql-42.2.23.jar dependency.

  • "javac -cp Music/postgresql-42.2.23.jar serverinsert.java" - works, as syntax is correct
  • "java -cp .:Music/postgresql-42.2.23.jar:serverinsert" - does not work, because serverinsert is not a .jar file (see also this)
  • "java -cp .:Music/postgresql-42.2.23.jar serverinsert" - normally it shall work, but you did not post your "serverinsert" code. Instead we have "writedbtst", which I believe is your entry point (method main) and you shall run it instead of "serverinsert". You could save it as "serverinsert.java" if it was not public.

To sum up, If it works in your IDE, try running it on the same platform using command line and replacing main class accordingly. And please follow java class naming conventtion.

1
  • yes it should be serverinsert insead of writedbtst.that was a mistake while pasting the code.still the same issue. Commented Sep 15, 2021 at 9:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.