2

I am facing problem while debugging my Java source code using jdb(Java Debugger). when i try to set value in instance variable using scanner class object it will display following output

Jdb Displaying following output

I cant understand why i am getting this message "unrecognized command". My java Source code is as follows

import java.util.Scanner;
class StringPlay{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
    int t;
    // System.out.println("t");
    t=sc.nextInt();
    for(int l=0;l<t;l++){
            String s="",a="";
        int k=0,player=0;
        // System.out.println("s");
        s=sc.next();
        for(int i=0;i<s.length();i++){
            for(int j=i+1;j<s.length();j++){
                if((int)s.charAt(j)!=(int)s.charAt(i)){
                    // strcat(a,s[j]);
                    a+=s.charAt(j);
                    // k++;
                }
                if(j==s.length()-1){
                    s=a;
                    a="";
                    // string::replace(a,0,"");

                    player++;
                }
            }
        }
        if(player%2==0)System.out.println("player1");
        else System.out.println("player2");
    }

    }
}

Well I am using jdb version 1.8 (Java SE version 1.8.0_151). and java version "9.0.1" Java(TM) SE Runtime Environment (build 9.0.1+11) Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

and I am Working on Java in Ubuntu 16.04LTS.

Thanks in advance for helping me.

Victor
  • 31
  • 7
  • I appreciate you attempt to do it the men’s way, however, did you ever consider using an IDE like Eclipse, Netbeans, or IntelliJ instead of command line `jdb`? The win in productivity, especially when debugging is huge… – Holger Feb 05 '18 at 11:48
  • Hey thanks @Holger Well I don't try on eclipse or on other IDE – Victor Feb 05 '18 at 13:01

2 Answers2

1

Try feeding the Scanner an input string for debugging with jdb. Because your console input is being read by the jdb and not by the Scanner class.

You can try:

String myInput = "This is my input, that I want in my sc.";
Scanner sc = new Scanner(myInput);

Or you can feed it directly:

Scanner sc = new Scanner("This is my input, that I want in my sc.");
Anže Mur
  • 1,545
  • 2
  • 18
  • 37
0

Got this info from this post:

You're going to need to split up the running of your app, and the debugging in two different terminals.

The first terminal needs to just run the app, but to run it in a way that jdb can connect to it later. You can do this with this command: java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n MyClass

In your second debug window, you need to connect to your Java app by running: jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000

Now, in order to put in values so your Scanner can read it, go back to the first terminal (the one running the app) and enter the arguments as normal.

Any breakpoints you set up will hit in your second debugging window.

Hopefully that helps.

kahyoung
  • 21
  • 6