stanfordnlp/CoreNLP · error · RuntimeException
Couldn't use swing to portray semantic graph
Error message
Couldn't use swing to portray semantic graph
What it means
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.
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.
Example fix
// before java -cp stanford-corenlp.jar edu.stanford.nlp.trees.GrammaticalStructureConversionUtils -visualize ... // after java -Djava.awt.headless=false -cp stanford-corenlp.jar:stanford-corenlp-models.jar:... \ edu.stanford.nlp.trees.GrammaticalStructureConversionUtils ... // or drop -visualize and use -outputFormat conllx
Defensive patterns
Strategy: try-catch
Validate before calling
// Detect headless/unsupported env before enabling -visualize
boolean canVisualize = !GraphicsEnvironment.isHeadless()
&& Class.forName("edu.stanford.nlp.rte.gui.SemanticGraphVisualization", false, cl) != null; Try / catch
try {
convertTrees(args);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().contains("swing to portray")) {
log.warn("Visualization unavailable (headless or missing GUI classes); falling back to text output");
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.
Related errors
- Class at path
- Class is in classpath multiple times
- Could not create l1 minimizer! Reverting to l2.
- Could not load class at path:
- Could not load class in jar:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/0e6c37385dbf69b9.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/GrammaticalStructureConversionUtils.java:908
// System.out.println("----------- CCprocessed dependencies -----------");
if (generateOriginalDependencies) {
printDependencies(gs, gs.typedDependenciesCCprocessed(GrammaticalStructure.Extras.MAXIMAL), tree, conllx, false, opts.convertToUPOS);
} else {
printDependencies(gs, gs.typedDependenciesEnhancedPlusPlus(), tree, conllx, false, opts.convertToUPOS);
}
}
}
if (portray) {
try {
// put up a window showing it
Class sgu = Class.forName("edu.stanford.nlp.rte.gui.SemanticGraphVisualization");
Method mRender = sgu.getDeclaredMethod("render", GrammaticalStructure.class, String.class);
// the first arg is null because it's a static method....
mRender.invoke(null, gs, "Collapsed, CC processed deps");
} catch (Exception e) {
throw new RuntimeException("Couldn't use swing to portray semantic graph", e);
}
}
} // end for
} // end convertTrees
// todo [cdm 2013]: Take this out and make it a trees class: TreeIterableByParsing
static class LazyLoadTreesByParsing implements Iterable<Tree> {
final Reader reader;
final String filename;
final boolean tokenized;
final String encoding;
final Function<List<? extends HasWord>, Tree> lp;
public LazyLoadTreesByParsing(String filename, String encoding, boolean tokenized, Function<List<? extends HasWord>, Tree> lp) {
this.filename = filename;View on GitHub (pinned to 1b7edd19c4)