Skip to content

Issue #594: fix demo chart scan when the project path contains a space#1013

Merged
timmolter merged 1 commit into
developfrom
issue-594-demo-classpath-scan-space-in-path
Jul 20, 2026
Merged

Issue #594: fix demo chart scan when the project path contains a space#1013
timmolter merged 1 commit into
developfrom
issue-594-demo-classpath-scan-space-in-path

Conversation

@timmolter

Copy link
Copy Markdown
Member

Fixes #594.

The bug

The reporter's classpath was E:\FEE\2nd Term\XChart\... — note the space in 2nd Term. That space is the whole story:

  1. DemoChartsUtil resolved the chart package directory with new File(url.getFile()). URL.getFile() returns a percent-encoded path, so the space came back as %20 and the File pointed at a directory that does not exist.
  2. getClassesByFile returned an empty list on !dir.exists() — silently. No charts discovered.
  3. XChartDemo.createNodes therefore built a tree with no children, leaving the root node "XChart Example Charts" (a String user object) as a leaf.
  4. Clicking it hit valueChanged, which casts any leaf's user object to ChartInfoClassCastException: String cannot be cast to ChartInfo.

Verified directly:

url                              = file:/tmp/2nd%20Term/foo/
new File(url.getFile()).exists() = false
new File(url.toURI()).exists()   = true

This also explains the issue title — the tree looks empty, so users see no "file explorer".

Changes

  • Resolve through URI, not getFile() — for both the file and jar branches. The jar path is itself a URL, so it goes through new File(new URL(...).toURI()) rather than being chopped at the first /, which keeps spaces and Windows drive letters intact. The JarFile is now closed via try-with-resources (dropping the @SuppressWarnings("resource")).
  • Fail loudlygetAllDemoCharts() used to return null when the package was missing (an NPE waiting to happen in createNodes) and an empty list when nothing was found. Both now throw IllegalStateException with the offending URL, so this is diagnosable instead of mysterious.
  • Guard the castvalueChanged now checks nodeInfo instanceof ChartInfo. Category and root nodes carry a String, and either can be a leaf; that should not kill the EDT.
  • Null-guard on dir.listFiles().

Test

DemoChartsUtilTest copies the compiled demo classes into a temp directory whose name contains a space, points a parent-less URLClassLoader at it, and asserts the scan still finds charts. Confirmed red before the fix and green after:

# before
java.lang.IllegalStateException: No demo charts were found at:
  file:/var/folders/.../xchart%20demo%20classes.../org/knowm/xchart/demo/charts
# after
Tests run: 81, Failures: 0, Errors: 0, Skipped: 0

Two things worth flagging

  • DemoChartsTest had stopped compiling — it calls new ToolTips(chart), but that constructor now takes (Chart, boolean, ToolTipType). Repaired here as a one-liner, since it blocked running any test in this module.
  • The reason nobody noticed is that xchart-demo sets <maven.test.skip>true</maven.test.skip> in its pom and CI never overrides it, so none of this module's tests — including the new one — run in CI. I left that alone as out of scope for this issue, but it's worth deciding on separately: the comment says it exists to skip test compilation during release:prepare, which a release-profile-scoped setting could handle without disabling the tests everywhere.

Verified locally with mvn -pl xchart-demo test -Dmaven.test.skip=false: 81 tests, all passing.

🤖 Generated with Claude Code

The demo's classpath scan resolved the chart package directory from
URL.getFile(), which is percent-encoded. Any project path containing a
space pointed at a non-existent directory, so no charts were found, the
tree was built empty, and clicking its root node - a String user object
that is now a leaf - threw a ClassCastException on the EDT.

- resolve the package directory through URI instead of getFile(), for
  both the file and jar cases, and close the JarFile
- fail loudly when the package is missing or no charts are found rather
  than handing back an empty/null list
- check the node type in the tree listener instead of assuming every
  leaf carries a ChartInfo

Also repairs DemoChartsTest, which stopped compiling when the ToolTips
constructor changed; it went unnoticed because this module skips tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@timmolter
timmolter merged commit 982a801 into develop Jul 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't see the file explorer

1 participant