Skip to content

Classpath lookup discriminates on file extension (does not work with non .jar files) #13059

Open
@jodersky

Description

@jodersky

Compiler version

3.0.0 (but also tested with master from 2021-07-12)

Problem

Embedding the compiler in a file not ending with .jar does not respect the user's classpath (when called with -usejavacp)

The gist of it is that I have a fat jar bundled as an executable with a shell script header. Essentially, this file is both a valid shell script (starting with #!/bin/sh) and also a zip file, containing all of my application's classfiles. This is a very pleasant way to distribute final user programs which feel "static": single binary, no installation step required, no dependencies apart from the JVM.

Now, I'm trying to call the dotty compiler from this application, setting the "-usejavacp" flag, since I already have the whole scala lib bundled in my executable. Essentially, the executable is structured as follows:

#!/bin/sh
exec java  $JAVA_OPTS -cp "$0" '<main class>' "$@"
... contents of the fat jar

and the main method does the following:

def main(args: Seq[String]) = {
  val res = dotty.tools.dotc.Main.process(
    Array("-usejavacp") ++ args.toArray
  )
  if (res.hasErrors) sys.exit(1) else sys.exit(0)
}

The problem is that the compiler will give the following error:

Could not find package scala from compiler core libraries.
Make sure the compiler core libraries are on the classpath.

unless my executable file has an extension of ".jar" (which is not a very common thing on unix systems, as executables typically have no extension).

Expectation

File extensions should not influence how the classpath is handled.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions