Skip to content

fix(java): derive filename from class name - #120

Open
aiueokakikukeko18071999 wants to merge 1 commit into
usnavy13:mainfrom
aiueokakikukeko18071999:fix/java-dynamic-class-name
Open

fix(java): derive filename from class name#120
aiueokakikukeko18071999 wants to merge 1 commit into
usnavy13:mainfrom
aiueokakikukeko18071999:fix/java-dynamic-class-name

Conversation

@aiueokakikukeko18071999

Copy link
Copy Markdown

Description

Java requires the filename to match the public class name. The hardcoded Code.java caused compilation errors for any code declaring a differently named public class:

Code.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java

Since public class HelloWorld is the conventional way to write Java, and LLM-generated code almost always uses a descriptive class name, this made the Java runtime effectively unusable unless the model was explicitly instructed to name its class Code.

The class name is now extracted from the source and used for both the filename and the java invocation, falling back to Code when no class declaration is found.

No new dependencies. re is from the standard library.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Tested on arm64 (Ubuntu 24.04, OCI Ampere A1) against a locally built image.

  • Java with a named public class now compiles and runs

Request:

{"code": "public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello, World!\"); } }", "lang": "java"}

Response:

{"stdout": "Hello, World!\n", "stderr": "", "files": ["HelloWorld.class", "HelloWorld.java"]}
  • tests/unit passes: 514 passed, 5 skipped
  • black --check and flake8 --select=F pass on the modified file

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Java requires the filename to match the public class name. The hardcoded
Code.java caused compilation errors for any code declaring a differently
named public class, which is the conventional way to write Java and what
LLM-generated code almost always produces.

The class name is now extracted from the source and used for both the
filename and the java invocation, falling back to Code when no class
declaration is found.
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.

1 participant