{"record":{"id":"0e6c37385dbf69b9","repo":"stanfordnlp/CoreNLP","slug":"couldn-t-use-swing-to-portray-semantic-graph","errorCode":null,"errorMessage":"Couldn't use swing to portray semantic graph","messagePattern":"Couldn't use swing to portray semantic graph","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/GrammaticalStructureConversionUtils.java","lineNumber":908,"sourceCode":"          // System.out.println(\"----------- CCprocessed dependencies -----------\");\n\n          if (generateOriginalDependencies) {\n            printDependencies(gs, gs.typedDependenciesCCprocessed(GrammaticalStructure.Extras.MAXIMAL), tree, conllx, false, opts.convertToUPOS);\n          } else {\n            printDependencies(gs, gs.typedDependenciesEnhancedPlusPlus(), tree, conllx, false, opts.convertToUPOS);\n          }\n        }\n      }\n\n      if (portray) {\n        try {\n          // put up a window showing it\n          Class sgu = Class.forName(\"edu.stanford.nlp.rte.gui.SemanticGraphVisualization\");\n          Method mRender = sgu.getDeclaredMethod(\"render\", GrammaticalStructure.class, String.class);\n          // the first arg is null because it's a static method....\n          mRender.invoke(null, gs, \"Collapsed, CC processed deps\");\n        } catch (Exception e) {\n          throw new RuntimeException(\"Couldn't use swing to portray semantic graph\", e);\n        }\n      }\n\n    } // end for\n  } // end convertTrees\n\n\n  // todo [cdm 2013]: Take this out and make it a trees class: TreeIterableByParsing\n  static class LazyLoadTreesByParsing implements Iterable<Tree> {\n\n    final Reader reader;\n    final String filename;\n    final boolean tokenized;\n    final String encoding;\n    final Function<List<? extends HasWord>, Tree> lp;\n\n    public LazyLoadTreesByParsing(String filename, String encoding, boolean tokenized, Function<List<? extends HasWord>, Tree> lp) {\n      this.filename = filename;","sourceCodeStart":890,"sourceCodeEnd":926,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/GrammaticalStructureConversionUtils.java#L890-L926","documentation":"convertTrees, when asked to visualize (-visualize option), reflectively loads edu.stanford.nlp.rte.gui.SemanticGraphVisualization, a Swing-based GUI class. If the class is missing (e.g. running a stripped/older jar or a headless environment where Swing initialization fails) or invocation fails, the underlying exception is wrapped in this RuntimeException.","triggerScenarios":"Running GrammaticalStructureConversionUtils.convertTrees with the visualize flag set on a classpath that does not contain the rte/gui visualization classes (often excluded from distributable jars), or in a headless environment (java.awt.headless=true, no X server/display) so Swing cannot initialize.","commonSituations":"Running on a server/CI without a display; using a minimal CoreNLP jar without the GUI packages; older Java versions or Wayland/X11 mismatch blocking AWT; the optional SemanticGraphVisualization class renamed/moved in a newer release.","solutions":["Run on a machine with a display and without java.awt.headless=true, and add the full CoreNLP jar (or model+gui packages) to the classpath.","Disable the visualize option and instead print trees with -conllx/-collapsed output when running headlessly.","Implement your own visualization (e.g. export to Graphviz DOT or use the web-based Stanford parser demo output) instead of the Swing viewer."],"exampleFix":"// before\njava -cp stanford-corenlp.jar edu.stanford.nlp.trees.GrammaticalStructureConversionUtils -visualize ...\n// after\njava -Djava.awt.headless=false -cp stanford-corenlp.jar:stanford-corenlp-models.jar:... \\\n  edu.stanford.nlp.trees.GrammaticalStructureConversionUtils ...\n// or drop -visualize and use -outputFormat conllx","handlingStrategy":"try-catch","validationCode":"// Detect headless/unsupported env before enabling -visualize\nboolean canVisualize = !GraphicsEnvironment.isHeadless()\n  && Class.forName(\"edu.stanford.nlp.rte.gui.SemanticGraphVisualization\", false, cl) != null;","typeGuard":null,"tryCatchPattern":"try {\n  convertTrees(args);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"swing to portray\")) {\n    log.warn(\"Visualization unavailable (headless or missing GUI classes); falling back to text output\");\n  } else throw e;\n}","preventionTips":["Don't use -visualize on servers/CI; use text formats (-outputFormat conllx) or export to Graphviz instead.","Ship the full CoreNLP jar including GUI classes if visualization is required.","Ensure a display/X server is available and java.awt.headless is not forced true."],"tags":["swing","gui","reflection","headless-environment","classpath"],"backgroundTag":"missing-optional-dependency","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}