4

I'm discovering JShell and I discovered the imports added in by default:

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

After doing that I added my own import using the following command:

import java.lang.Math

Is there a way to remove the latter import without killing the active session/restarting?

I've tried to issue the /edit command, remove the import, click accept and click exit, but that didn't do the trick.

As stated in the comments, /reset removes the import, but it also removes anything else previously input in the session. Is there a specific way to ONLY remove the import statement?

Naman
  • 27,789
  • 26
  • 218
  • 353
Thibstars
  • 1,085
  • 1
  • 9
  • 30

2 Answers2

5

After some searching, I managed to find a solution. It's a combination of /list (to know which line to remove) and /drop.

/drop [name[ name...]|id[ id...]]

Drops a snippet, making it inactive. Provide either the name or the ID of an import, class, method, or variable. For more than one snippet, separate the names and IDs with a space. Use the /list command to see the IDs of code snippets.

jshell> import java.lang.Math

jshell> /list

   1 : import java.lang.Math;

jshell> /drop 1

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*
Naman
  • 27,789
  • 26
  • 218
  • 353
Thibstars
  • 1,085
  • 1
  • 9
  • 30
0

For those who are searching for a way to auto-import the common libraries:

jshell JAVASE

That will import ~200 packages automatically. Like we havent't had seen enough of unworldly design decisions in the Java world, a smile may disappear when addressing the first List:

jshell> List<Duration> durations = new ArrayList<>();
|  Error:
|  reference to List is ambiguous
|    both class java.awt.List in java.awt and interface java.util.List in java.util match
|  List<Duration> durations = new ArrayList<>();
|  ^--^
|  Error:
|  reference to Duration is ambiguous
|    both class javax.xml.datatype.Duration in javax.xml.datatype and class java.time.Duration in java.time match
|  List<Duration> durations = new ArrayList<>();
|       ^------^

That can be fixed, but one needs to create a startup script:

  1. Start jshell JAVASE
  2. Show imports: /imports
  3. Copy that to a file, remove all lines with java.awt and other unwanted imports
  4. Save these imports to a file jshell-imports
  5. Start jshell jshell-imports
jshell> List<Duration> durations = new ArrayList<>();
durations ==> []

Of course, the file has to be recreated for each Java update.

steffen
  • 16,138
  • 4
  • 42
  • 81