Bug 558545 - support shebang style java
Summary: support shebang style java
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.15   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2019-12-21 11:38 EST by Max Rydahl Andersen CLA
Modified: 2020-01-15 05:54 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Max Rydahl Andersen CLA 2019-12-21 11:38:08 EST
it would be great if eclipse JDT would support shebang style scripting of java.

https://blog.codefx.org/java/scripting-java-shebang/

#!java --source 11

import java.util.Arrays;

public class script {
 
    public static void main(String[] args) {
        System.out.println("Hello, Java scripts!");
        Arrays.stream(args)
            .forEach(arg -> System.out.println(arg))
    
} 


helloworld example, save this as helloworld and chmod +x the file, now you can do ./helloworld.


The following are issues I bumped into trying to use eclipse with such java class.

1) if file is named .java and in a java project with folder as root folder then content assist etc. works but complains about the shebang (!#) at top.

2) debugging is possible using connect to remote javavm but source lookup does not work

3) found no  way to run this script from launch configuration using java launcher.
Comment 1 Andrey Loskutov CLA 2020-01-15 05:20:59 EST
That would be cool, yes.

For the "#!java --source 11" we could contribute a content describer that "detects" this as "Java" content type, even it is called "*.sh".

For the shebang error in file: we probably need to change some parser rules?

For running / debugging: I believe as soon as we "know" it is a JavaElement containing "main", we will offer the run/debug possibility.
Comment 2 Max Rydahl Andersen CLA 2020-01-15 05:54:23 EST
for what it is worth since I opened this issue I created jbang (https://github.com/maxandersen/jbang) and during that i realized you don't actually need the #! style, you can just do:

//usr/bin/env java "$0" "$@" ; exit $? 
import java.util.Arrays;

public class script {
 
    public static void main(String[] args) {
        System.out.println("Hello, Java scripts!");
        Arrays.stream(args)
            .forEach(arg -> System.out.println(arg));
    }
} 


This is similar to how golang solves it.

I would say with that realization it is not really required to support it as its afaik not even part of JLS but just a "quick hack" in the java binary.