Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error ANT02: Cannot find Java executable, currently set to '/usr/bin/java' #30

Closed
martin-shields-sage opened this issue Apr 13, 2022 · 13 comments

Comments

@martin-shields-sage
Copy link

This is happening trying to update to version 10 and running this package in a docker container with a .net solution.

Typical build instruction would be :

RUN dotnet build *****.csproj -c "*****" -o /app/build -p:AntlrvsixJavaExec="${JAVA_HOME}/bin/java"

AntlrvsixJavaExec is a path we are using that points to the java executable and this works with previous changes. Has this property changed at all do you know?

@kaby76
Copy link
Owner

kaby76 commented Apr 13, 2022

Thanks for the info.

I've been trying to make Antlr4BuildTasks not have any dependencies, and include both the most recent Antlr tool jar (now 4.10) and a JRE for Windows. This idea is ok for the Antlr tool jar because I can download the file from Maven Central another version, and I think a probe path for something else.

For the JRE, I've included a version for Windows, and assume a hardwired location for non-Windows. This probably isn't going to work because I really don't want to create JRE's for every architecture and add it to the Nuget package--it's just too much junk in one file. I will need to fish around to find out who's has a server full of JRE's so I can just fetch the right one if I can't find one on the current path. And, even that may not work depending on which version of the Antlr jar one is using.

Point taken. I will add something back in to indicate where to find Java because I'll not likely be able to pick out the right JRE. But it'll likely not be called "AntlrvsixJavaExec" because it has nothing to do with the old Antlr Visual Studio Extension, and instead call it JavaExec.

@martin-shields-sage
Copy link
Author

Thanks for the update!

Just another note is that our docker container is using the Microsoft image which I believe is alpine linux (I could be wrong) and not windows.

We are using https://hub.docker.com/_/microsoft-openjdk-jdk to get a jdk into our docker image and I have tried pointing this to /usr/bin/java with not a lot of success

@martin-shields-sage
Copy link
Author

Just a bit of an update, I was able to point the JDK to /usr/bin/java using this:
=> [build-netcore 2/7] COPY --from=mcr.microsoft.com/openjdk/jdk:17-ubuntu /usr/lib/jvm/msopenjdk-17-amd64/bin/java /usr/bin/java

My problem now is that I have a different error message:

/root/.nuget/packages/antlr4buildtasks/10.0.0/build/Antlr4BuildTasks.targets(131,3): error ANT02: The Antlr4 tool failed.

I am wondering if my docker container is maybe missing some required files for this?

@kaby76
Copy link
Owner

kaby76 commented Apr 13, 2022

If you hang on, I will update the code and release soon. There's been kind of a mess going on with the Antlr grammars-vr repo with the new 4.10 Antlr release, but I'll get this done now.

kaby76 added a commit that referenced this issue Apr 13, 2022
@kaby76
Copy link
Owner

kaby76 commented Apr 13, 2022

Hi Martin,

I have a solution that might work for you. Basically it involves a mod to your .csproj file for <JavaExec />. See the code below.

<!-- Template generated code from Antlr4Templates v 1.3 -->
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>AntlrTemplate</RootNamespace>
  </PropertyGroup>
  
  <ItemGroup>
    <Antlr4 Include="Arithmetic.g4">
      <Package>myapp</Package>
      <JavaExec>c:/Program Files/Java/jdk-11.0.4/bin/java.exe</JavaExec>
    </Antlr4>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Antlr4.Runtime.Standard" Version="4.10" />
    <PackageReference Include="Antlr4BuildTasks" Version="10.1" PrivateAssets="all" />
  </ItemGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <NoWarn>3021;1701;1702</NoWarn>
  </PropertyGroup>

</Project>

So, here the location of the Java executable is the full path file name. The reason it's in the .csproj because I expect the Antlr tool .jar file to be Java version dependent at some point. They're likely going to upgrade the version of Java used to build the tool. This solution of having it in the .csproj file isn't exactly "OS independent" .csproj though. But, it is in the UI when you use the Visual Studio IDE and look at the properties of the .g4 in the project explorer. You might be able to pass the parameter with an environmental variable.

Anyways, I just released a version with this, 10.1 Antlr4BuildTasks.

@IRebo
Copy link

IRebo commented May 10, 2022

To make things more interesting. Would it be possible to add a check (or a setting) that detects that installed OS java version is compatible (not too old) and use the bundled one in such case ?
We still have java 8.0 installed for compatibility reasons as our primary java and its not compatible with the jars used by antlr4.

@kaby76
Copy link
Owner

kaby76 commented May 10, 2022

To make things more interesting. Would it be possible to add a check (or a setting) that detects that installed OS java version is compatible (not too old) and use the bundled one in such case ? We still have java 8.0 installed for compatibility reasons as our primary java and its not compatible with the jars used by antlr4.

I agree. I'll have to go through and see what the min Java versions are required per Antlr4 tool and runtime versions (note, they can be different). Then, I'll have to check the version by running java. I'm probably going to add code to download a JRE from OpenJDK if the java version on path or specified in the .csproj isn't workable.

@Ozzard
Copy link

Ozzard commented Jul 4, 2022

@kaby76 - Ken, is the assurance in the README that the tool will download a JRE into .jre now out of date? This just bit me - if it is, I'll submit a PR with some revised wording if you don't want to fix it yourself. If not, something weird just happened to me and the .m2 downloaded and the .jre didn't!

Lovely, lovely tool - thank you for this!

@kaby76
Copy link
Owner

kaby76 commented Jul 4, 2022

It should be downloading the JRE, but won't if

  • you have it set in the .csproj in the <Antlr4> element, with <JavaExec>full-path-of-java-executable</JavaExec>. That's passed on here to the tool, and checked in the tool code here.
  • it can find a java executable on the path. That's checked here.

I don't think I say that in quite so many words though.

It is a nice tool, but kudos really should go to Harwell who somehow figured out how to do this via a package reference to nuget. Really a remarkable insight. I just been hacking at it to clean up some of the rough edges.

@renzosilv
Copy link

Sorry to piggy back off of this question but I seem to be having the same issue all of the sudden. (Just migrated to a new PC)

0>Antlr4BuildTasks.targets(138,9): Error ANT02 : End of Central Directory record could not be found.   at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
   at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding, Boolean overwriteFiles)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName)
   at Antlr4.Build.Tasks.RunAntlrTool.DecompressJava(String uncompressed_root_dir, String archive_name)
0>Antlr4BuildTasks.targets(138,9): Error ANT02 : Cannot find Java executable''   at Antlr4.Build.Tasks.RunAntlrTool.SetupJava()
   at Antlr4.Build.Tasks.RunAntlrTool.Execute()
0>Antlr4BuildTasks.targets(138,9): Error ANT02 : The Antlr4 tool failed.

This is my project file:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <Nullable>enable</Nullable>
        <LangVersion>9</LangVersion>
        <NoWarn>$(NoWarn);NU1605;CS3021</NoWarn>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>

    <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
      <PlatformTarget>x64</PlatformTarget>
    </PropertyGroup>

    <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
      <PlatformTarget>x64</PlatformTarget>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Antlr4BuildTasks" Version="12.2.0" PrivateAssets="all"/>
        <PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.1"/>
        <PackageReference Include="log4net" Version="2.0.15"/>
        <PackageReference Include="MySql.Data" Version="8.1.0"/>
        <PackageReference Include="SharpCompress" Version="0.34.1">
            <Aliases>HideZip</Aliases>
        </PackageReference>
        <PackageReference Include="YamlDotNet" Version="13.5.1"/>

        <ProjectReference Include="..\Utilities\Utilities.csproj"/>
    </ItemGroup>

    <ItemGroup>
      <Antlr4 Include="AsciiTestPattern\Grammar\atp.g4" />
      <Antlr4 Include="StandardTestInterfaceLanguage\Grammar\stil.g4" />
      <Antlr4 Include="UniversalPinTable\Grammar\upt.g4" />
    </ItemGroup>

    <ItemGroup>
      <Compile Remove="obj\**" />
    </ItemGroup>

    <ItemGroup>
      <EmbeddedResource Remove="obj\**" />
    </ItemGroup>

    <ItemGroup>
      <None Remove="obj\**" />
    </ItemGroup>

</Project>

Any thoughts on why this is not longer building?

@renzosilv
Copy link

Actually - I had the wrong format earlier.

This fixed it. Is that how it should be declared going forwards ?

      <Antlr4 Include="AsciiTestPattern\Grammar\atp.g4" ><JavaExec>C:\Program Files\Java\jdk-21\bin/java.exe</JavaExec></Antlr4>
      <Antlr4 Include="StandardTestInterfaceLanguage\Grammar\stil.g4" ><JavaExec>C:\Program Files\Java\jdk-21\bin/java.exe</JavaExec></Antlr4>
      <Antlr4 Include="UniversalPinTable\Grammar\upt.g4" ><JavaExec>C:\Program Files\Java\jdk-21\bin/java.exe</JavaExec></Antlr4>
    </ItemGroup>

@kaby76
Copy link
Owner

kaby76 commented Oct 5, 2023

I'll publish the next release today. It should have the fix. If not let me know. Ken

@kaby76
Copy link
Owner

kaby76 commented Oct 5, 2023

Actually - I had the wrong format earlier.

This fixed it. Is that how it should be declared going forwards ?

      <Antlr4 Include="AsciiTestPattern\Grammar\atp.g4" ><JavaExec>C:\Program Files\Java\jdk-21\bin/java.exe</JavaExec></Antlr4>
      <Antlr4 Include="StandardTestInterfaceLanguage\Grammar\stil.g4" ><JavaExec>C:\Program Files\Java\jdk-21\bin/java.exe</JavaExec></Antlr4>
      <Antlr4 Include="UniversalPinTable\Grammar\upt.g4" ><JavaExec>C:\Program Files\Java\jdk-21\bin/java.exe</JavaExec></Antlr4>
    </ItemGroup>

Yes that should be fine....JavaExec. Alternatively I think it looks for a java executable on PATH. It does check to make sure the executable is at least version 11 or more recent. Otherwise the Antlrr java .jar tool cannot work.

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

No branches or pull requests

5 participants