kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
3.95k stars 192 forks source link

can't open kaitai-struct-compiler on win10 #822

Open bitlgy opened 3 years ago

bitlgy commented 3 years ago

Have downloaded universal version to upzip. And have installed JRE. when execute kaitai-struct-compiler, got a error" can not find or load main class io.kaitai.struct.JavaMain".

dgelessus commented 3 years ago

Can you run kaitai-struct-compiler -v --version, copy the entire output of the command, and post it here? This will show some extra information about how exactly Java is being called, which will help us with debugging the problem. Thanks!

golfinq commented 3 years ago

Hello, I had this same issue and running bin\kaitai-struct-compiler.bat -v --version from the root folder yielded the same error.

golfinq commented 3 years ago

I just checked and installing java inside of WSL ubuntu then running bin/kaitai-struct-compiler inside WSL ubuntu works.

dgelessus commented 3 years ago

@golfinq If you run the compiler with -v --version, there should be more output before the "Could not find or load main class" error. Could you run the command again under Windows and post the entire output?

It's good that the compiler works with Java installed in WSL as a workaround, but it's also supposed to run normally with Java installed on Windows directly. If that doesn't work, we would like to know what exactly is going wrong, so we can hopefully fix the problem 🙂

golfinq commented 3 years ago

@dgelessus I also included the java version I am using, if it would help. There is no output outside of the error message even with the extra command line options.

PS <root>\kaitai-struct-compiler-0.9> java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

PS <root>\kaitai-struct-compiler-0.9> .\bin\kaitai-struct-compiler.bat -v --version
Error: Could not find or load main class io.kaitai.struct.JavaMain
generalmimon commented 3 years ago

I also use a Windows 10 machine, so I'll see if I can reproduce the issue and dig up more details.

generalmimon commented 3 years ago

@bitlgy @golfinq

I'll see if I can reproduce the issue

Despite my best efforts, I can't reproduce it, there must be something else on your machines. I even grabbed a clean computer with Win 10 basically with a factory configuration (it doesn't even have java installed) and tried the whole KSC installation procedure from scratch. I wrote down the exact process step by step, please indicate whether you did something differently:

  1. Go to https://kaitai.io/#download, choose Universal .zip and click the green Download button with stable v0.9, 9.5 MiB next to it.

  2. Extract downloaded file C:\Users\pc\Downloads\kaitai-struct-compiler-0.9.zip to folder ~\Downloads\kaitai-struct-compiler-0.9\

  3. Press ⊞ Win + R, type powershell and Enter ↵

  4. PS C:\Users\pc> cd .\Downloads\kaitai-struct-compiler-0.9\
    PS C:\Users\pc\Downloads\kaitai-struct-compiler-0.9> .\bin\kaitai-struct-compiler.bat --version
    A Java JDK is not installed or can't be found.
    
    Please go to
     http://www.oracle.com/technetwork/java/javase/downloads/index.html
    and download a valid Java JDK and install before running kaitai-struct-compiler.
    
    If you think this message is in error, please check
    your environment variables to see if "java.exe" and "javac.exe" are
    available via JAVA_HOME or PATH.

    Note: I actually don't understand why it says download a valid Java ~JDK~ and see if "java.exe" and ~"javac.exe"~ are available, when it requires only java.exe (not ~javac.exe~). So just a Java JRE will suffice. We should fix this so that we don't declare dependencies that it does not depend on.

  5. Go to https://java.com/en/download/

    64-bit Java for Windows

    Recommended Version 8 Update 271 (filesize: 79.5 MB)

    Release date October 20, 2020

  6. Click the red button Agree and Start Free Download

  7. Got jre-8u271-windows-x64.exe in Downloads/ folder -> launch it

  8. Click Install, wait for it to finish and click Close

    Note: Got C:\Program Files (x86)\Common Files\Oracle\Java\javapath in system Path (which contains java.exe)

  9. Restart Powershell and repeat the procedure:

    PS C:\Users\pc> cd .\Downloads\kaitai-struct-compiler-0.9\
    
    PS C:\Users\pc\Downloads\kaitai-struct-compiler-0.9> java -version
    java version "1.8.0_271"
    Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
    Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
    
    PS C:\Users\pc\Downloads\kaitai-struct-compiler-0.9> .\bin\kaitai-struct-compiler.bat --version
    kaitai-struct-compiler 0.9

The error Could not find or load main class io.kaitai.struct.JavaMain is most likely an issue with Java classpath. The class io.kaitai.struct.JavaMain is located in the archive kaitai-struct-compiler-0.9\lib\io.kaitai.kaitai-struct-compiler-0.9.jar, and this archive is supposed to be passed to the java executable via the -cp option, but that apparently doesn't work for some reason.

@golfinq Can you try something? It would be useful for me if I could see the full java invocation, so please open the file bin\kaitai-struct-compiler.bat in a text editor, find the line

"%_JAVACMD%" !_JAVA_OPTS! !KAITAI_STRUCT_COMPILER_OPTS! -cp "%APP_CLASSPATH%" %MAIN_CLASS% !_APP_ARGS!

and surround it with @echo on and @echo off, like this:

@echo on
"%_JAVACMD%" !_JAVA_OPTS! !KAITAI_STRUCT_COMPILER_OPTS! -cp "%APP_CLASSPATH%" %MAIN_CLASS% !_APP_ARGS!
@echo off

Save it, run it again with .\bin\kaitai-struct-compiler --version and then please share the output. Here's mine (just added line breaks for readability):

PS C:\Users\pc\Downloads\kaitai-struct-compiler-0.9> .\bin\kaitai-struct-compiler --version

C:\Users\pc\Downloads\kaitai-struct-compiler-0.9>"java" !_JAVA_OPTS! !KAITAI_STRUCT_COMPILER_OPTS!
-cp "C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\io.kaitai.kaitai-struct-compiler-0.9.jar;
C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\org.scala-lang.scala-library-2.12.4.jar;
C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\com.github.scopt.scopt_2.12-3.6.0.jar;
C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\com.lihaoyi.fastparse_2.12-1.0.0.jar;
C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\com.lihaoyi.fastparse-utils_2.12-1.0.0.jar;
C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\com.lihaoyi.sourcecode_2.12-0.1.4.jar;
C:\Users\pc\Downloads\kaitai-struct-compiler-0.9\bin\\\..\lib\\org.yaml.snakeyaml-1.25.jar"
io.kaitai.struct.JavaMain !_APP_ARGS!

kaitai-struct-compiler 0.9

But of course, my output is pretty irrelevant, because it works for me 😄

generalmimon commented 3 years ago

I've just figured out that the batch file bin\kaitai-struct-compiler.bat is actually not under our control, it's from file bat-template in sbt/sbt-native-packager repo.

That gives us some support possibilities - we can search their issue tracker or Gitter if someone else using the sbt-native-packager didn't have the same problem, or open an issue there ourselves if we don't figure out the problem here.

KOLANICH commented 3 years ago

@bitlgy,

  1. try to open cmd, then type echo %JAVA_HOME%
  2. if JAVA_HOME is not set, try to set it set JAVA_HOME=<path to java without bin/java.exe> and then retry
  3. If that succeeds, set that into permanent environment variables in system configuration
  4. also you may use another JRE impl, ie. GraalVM even without full installation. You need to set JAVA_HOME to the needed dir.
generalmimon commented 3 years ago

2. if JAVA_HOME is not set

I don't think the problem is that the java executable was not found (i.e. java.exe is neither in %Path% nor in %JAVA_HOME%/bin/), because the Error: Could not find or load main class io.kaitai.struct.JavaMain comes from java executable.

If the shell didn't know what java is, the error would look like this (just not foo, but java):

C:\Users\pc>foo
'foo' is not recognized as an internal or external command,
operable program or batch file.

set JAVA_HOME=<path to java without java.exe>

This is a weird advice: JAVA_HOME should point to the root of JDK installation, which is by default C:\Program Files\Java\jdk1.8.0_271, and this directory doesn't directly contain the java.exe - it is in %JAVA_HOME%/bin/java.exe (note the bin/).

But setting JAVA_HOME is not necessary, as well as installing JDK (JRE is enough, as you can read from my previous https://github.com/kaitai-io/kaitai_struct/issues/822#issuecomment-727569435).

4. also you may use another JRE impl, ie. GraalVM even without full installation. You need to set JAVA_HOME to the needed dir.

I really doubt that it would change anything.

golfinq commented 3 years ago

@generalmimon Here is the output from the powershell after editing the batch file (EDIT: added newlines for readability)

PS C:\Users\pc\Software\kaitai-struct-test\kaitai-struct-compiler-0.9> .\bin\kaitai-struct-compiler.bat --version

C:\Users\pc\Software\kaitai-struct-test\kaitai-struct-compiler-0.9>"java" !_JAVA_OPTS! !KAITAI_STRUCT_COMPILER_OPTS! -cp 
"C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\io.kaitai.kaitai-struct-compiler-0.9.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\org.scala-lang.scala-library-2.12.4.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.github.scopt.scopt_2.12-3.6.0.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.lihaoyi.fastparse_2.12-1.0.0.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.lihaoyi.fastparse-utils_2.12-1.0.0.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.lihaoyi.sourcecode_2.12-0.1.4.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\org.yaml.snakeyaml-1.25.jar" io.kaitai.struct.JavaMain !_APP_ARGS!
Error: Could not find or load main class io.kaitai.struct.JavaMain
PS C:\Users\pc\Software\kaitai-struct-test\kaitai-struct-compiler-0.9>

The path C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\ doesn't exist, as I changed the folder names. I suspect that if you change the folder names, it will no longer work, even if you redownload the universal zip and unzip it somewhere else (as I have here). Unfortunately I don't have a clean install of windows to test this.

I recreated the folder C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\ with all the jar files in it and it worked.

PS C:\Users\pc\Software\kaitai-struct-test\kaitai-struct-compiler-0.9> .\bin\kaitai-struct-compiler.bat --version

C:\Users\pc\Software\kaitai-struct-test\kaitai-struct-compiler-0.9>"java" !_JAVA_OPTS! !KAITAI_STRUCT_COMPILER_OPTS! -cp 
"C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\io.kaitai.kaitai-struct-compiler-0.9.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\org.scala-lang.scala-library-2.12.4.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.github.scopt.scopt_2.12-3.6.0.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.lihaoyi.fastparse_2.12-1.0.0.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.lihaoyi.fastparse-utils_2.12-1.0.0.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\com.lihaoyi.sourcecode_2.12-0.1.4.jar;
C:\Users\pc\Software\vbf\kaitai-struct-compiler\\lib\\org.yaml.snakeyaml-1.25.jar" io.kaitai.struct.JavaMain !_APP_ARGS!
kaitai-struct-compiler 0.9
PS C:\Users\pc\Software\kaitai-struct-test\kaitai-struct-compiler-0.9>
generalmimon commented 3 years ago

@golfinq Thanks for the PowerShell output, the problem really is there. I wonder how it happened. I know that the directory is not hardcoded during the unzipping (not that it would even possible to trigger any script just by unzipping a ZIP archive), it's resolved on the fly in the batch file. So I believe the problem is almost certainly here (sbt-native-packager / ... / bat-template:13-22):

if "%KAITAI_STRUCT_COMPILER_HOME%"=="" (
  set "APP_HOME=%~dp0\\.."

  rem Also set the old env name for backwards compatibility
  set "KAITAI_STRUCT_COMPILER_HOME=%~dp0\\.."
) else (
  set "APP_HOME=%KAITAI_STRUCT_COMPILER_HOME%"
)

set "APP_LIB_DIR=%APP_HOME%\lib\"

As you can see, APP_HOME depends on whether the environment variable KAITAI_STRUCT_COMPILER_HOME is set or not. If not, it's resolved as ..\ from the location of kaitai-struct-compiler.bat, and that would work for you. But that doesn't happen, so KAITAI_STRUCT_COMPILER_HOME apparently is set on your computer - please run echo $env:KAITAI_STRUCT_COMPILER_HOME from PowerShell if I'm right.

This environment variable probably got set from some previous Windows .msi installation, which permanently sets some env vars. There is also set "KAITAI_STRUCT_COMPILER_HOME=%~dp0\\.." in the above excerpt, but that sets the var only for the batch session, it lives only as long as the batch file is running.

generalmimon commented 3 years ago

This environment variable probably got set from some previous Windows .msi installation

Indeed, I tried to install KSC with the kaitai-struct-compiler.msi installer and it does the following permanent changes to the system environment variables (diffing set > env-vars(...).txt run in cmd.exe):

--- 1/env-vars-before-ksc-0.9.msi-installation.txt
+++ 2/env-vars-after-ksc-0.9.msi-installation.txt
@@ -10,8 +10,8 @@ DriverData=C:\Windows\System32\Drivers\DriverData
 GOPATH=C:\Users\pc\go;C:\Go
 HOMEDRIVE=C:
 HOMEPATH=\Users\pc
-JAVA_HOME=C:\Program Files\Java\jdk1.8.0_231
 JAVA_OPTS=-Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8
+KAITAI_STRUCT_COMPILER_HOME=C:\Program Files (x86)\kaitai-struct-compiler\
 LOCALAPPDATA=C:\Users\pc\AppData\Local
 LOGONSERVER=\\DESKTOP-MKDR147
 MYSQLCONNECTOR_ASSEMBLIESPATH=C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5
@@ -77,6 +77,7 @@ Path=
        C:\Program Files\Docker\Docker\resources\bin;
        C:\ProgramData\DockerDesktop\version-bin;
        C:\bin;
+       C:\Program Files (x86)\kaitai-struct-compiler\bin;
        C:\Users\pc\AppData\Local\Programs\Python\Python38-32\Scripts\;
        C:\Users\pc\AppData\Local\Programs\Python\Python38-32\;
        C:\Users\pc\.cargo\bin;

So if you also ran the MSI installer before, it would all make sense.


Edit: I've also verified that uninstalling the KSC .msi package (from ⊞ Win + R: control.exe /name Microsoft.ProgramsAndFeatures) properly restores the env vars to their original state:

--- 1/env-vars-after-ksc-0.9.msi-installation.txt
+++ 2/env-vars-after-ksc-0.9.msi-uninstallation.txt
@@ -11,7 +11,6 @@ GOPATH=C:\Users\pc\go;C:\Go
 HOMEDRIVE=C:
 HOMEPATH=\Users\pc
 JAVA_OPTS=-Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8
-KAITAI_STRUCT_COMPILER_HOME=C:\Program Files (x86)\kaitai-struct-compiler\
 LOCALAPPDATA=C:\Users\pc\AppData\Local
 LOGONSERVER=\\DESKTOP-MKDR147
 MYSQLCONNECTOR_ASSEMBLIESPATH=C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5
@@ -77,7 +76,6 @@ Path=
        C:\Program Files\Docker\Docker\resources\bin;
        C:\ProgramData\DockerDesktop\version-bin;
        C:\bin;
-       C:\Program Files (x86)\kaitai-struct-compiler\bin;
        C:\Users\pc\AppData\Local\Programs\Python\Python38-32\Scripts\;
        C:\Users\pc\AppData\Local\Programs\Python\Python38-32\;
        C:\Users\pc\.cargo\bin;
bitlgy commented 3 years ago

@KOLANICH I find out the reason generating the problem. Sorry, I had set the wrong path for KAITAI_STRUCT_COMPILER_HOME. KAITAI_STRUCT_COMPILER_HOME should be in D:\Program Files (x86)\kaitai-struct-compiler , but i set it to C:\Program Files (x86)\kaitai-struct-compiler .

When I modify it into the correct path, It's working very well. image

I'm so sorry for the trouble.

bitlgy commented 3 years ago

Great! You had found out the problem reason.

generalmimon commented 3 years ago

@bitlgy No offense, but...

(to myself) Why do I write these long descriptive comments when people don't read them?

bitlgy commented 3 years ago

@bitlgy No offense, but...

(to myself) Why do I write these long comments when people don't read them?

@generalmimon I took some time to read them and figure out them. I think I need to do more practice on English. hahaha~~~

golfinq commented 3 years ago

@generalmimon This was the issue, once I uninstalled the program and restarted (for good measure) the paths were correct and it worked properly.