Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT Security NachrichtenThe Rising Threat of Deepfakes: Why Organizations Must Rethink Trust(23.09.2026 um 02:00 Uhr)
Sichere ProgrammierungI built an agent that refuses to answer Next.js from stale docs(24.09.2026 um 03:17 Uhr)
Sichere ProgrammierungI vibe-coded a Next.js knowledge base that argues with itself(24.09.2026 um 03:17 Uhr)
Linux Tipps & HardeningUbuntu speeds up kernel security updates because of AI(24.09.2026 um 03:01 Uhr)
IT Security NachrichtenGoogle's PageBreak Project – Real-World Findings(24.09.2026 um 02:00 Uhr)
IT Security NachrichtenThe Rising Threat of Deepfakes: Why Organizations Must Rethink Trust(23.09.2026 um 02:00 Uhr)
Sichere ProgrammierungI built an agent that refuses to answer Next.js from stale docs(24.09.2026 um 03:17 Uhr)
Sichere ProgrammierungI vibe-coded a Next.js knowledge base that argues with itself(24.09.2026 um 03:17 Uhr)
Linux Tipps & HardeningUbuntu speeds up kernel security updates because of AI(24.09.2026 um 03:01 Uhr)
IT Security NachrichtenGoogle's PageBreak Project – Real-World Findings(24.09.2026 um 02:00 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

System.in, Scanner, and File Descriptors – JAVA Case study

Hello, Everyone, I am currently learning Java. As a beginner, I worked with console IO. To read input from the terminal, I use the Scanner class. I tried a mini CLI project, so I used the Scanner object in different classes. For resource…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Hello, Everyone, I am currently learning Java. As a beginner, I worked with console IO. To read input from the terminal, I use the Scanner class.



I tried a mini CLI project, so I used the Scanner object in different classes. For resource management, the Scanner object — especially when reading from sources like System.in or files — utilizes underlying system resources (like file descriptors or input streams). Closing the Scanner explicitly releases these resources back to the operating system, preventing resource leaks.



So, I closed the Scanner in each class, but after running my program, I got this exception:




Exception in thread "main" java.util.NoSuchElementException






This was the first time I saw this exception. After reading some related Stack Overflow discussions, I understood the real reason.









The Core Problem: Input Stream Closure



When a Scanner is closed, it also closes the underlying input stream it's connected to.

For example, if you create a Scanner from a FileInputStream, closing the Scanner will also close that FileInputStream. This ensures that all related resources are properly shut down.



But I wondered:




“If I create a new Scanner again, shouldn’t the JVM reopen the input stream?”




Surprisingly, no — and here’s why.







Example Code





import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan1 = new Scanner(System.in);
String ip1 = scan1.next();
scan1.close();

Scanner scan2 = new Scanner(System.in);
String ip2 = scan2.next(); // throws Runtime exception
scan2.close();
}
}





Output:




Exception in thread "main" java.util.NoSuchElementException












What Actually Happens Under the Hood





  • System.in is a singleton InputStream provided by the JVM — not something that can be reopened automatically.

  • When you call scan1.close(), it calls System.in.close() internally.

  • This closes the standard input stream permanently for that JVM session.

  • Any subsequent Scanner (like scan2) tries to read from a closed input stream, so the JVM throws a NoSuchElementException or IllegalStateException.



So even though you created a new Scanner, it still points to the same closed System.in, not a fresh one.









What’s the Correct Practice?



If you’re reading from System.in, never close the Scanner until your entire program finishes reading all input.

You can safely reuse the same Scanner across methods or classes by passing it as a reference — but avoid closing it.



Example fix:




import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

String ip1 = scan.next();
String ip2 = scan.next();

scan.close(); // Close only once at the end
}
}












JVM and OS-Level Insight





  • System.in is typically linked to stdin (file descriptor 0) at the OS level.

  • Once closed, the OS releases that descriptor, and the JVM does not recreate it automatically.

  • Creating a new Scanner doesn’t reopen the descriptor — it just wraps the already-closed stream.









Key Takeaway




In Java, closing a Scanner created from System.in closes the entire standard input stream for the running JVM.

Always close it only once, at the end of the program — or better, let the JVM handle it automatically when the program exits.







That’s what I discovered in my small case study — a good reminder that not all resources can be reopened once closed, especially when they represent system-level standard streams.

SOC Incident Playbook: Remote Code Execution (RCE) Defense
title: Detect Exploitation - System.in, Scanner, and File Descriptors – JAVA Case study
id: 2e81aa27-3164-4300-a9ed-52527c1e290b
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "System.in, Scanner, and File D" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich System.in, Scanner, and File Descriptors.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten System.in, Scanner, and File Descriptors – JAVA Case study

Thematisch verwandte Begriffe: Systemin, Scanner, File, Descriptors · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96676 | A vulnerability was identified in Fast FAC1900R 20190827_2.0.2. The impa…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick