• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Maven cannot start Java Virtual Machine

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear community,

This is an adapted post from Java Programming Forums, where it is titled ‘Java installed but not listed in Windows 10 Apps’.

I was following a Maven fundamentals course on Pluralsight, but I got stuck there. (It doesn’t work. For those interested : www.javaprogrammingforums.com/java-ides/44585-mvn-maven-not-recognized-command.html). So I switched to www.tutorialspoint.com/maven/index.htm.

I did not exactly follow the setup steps since I had already done that sort of stuff (like setting PATH variables). Then, when I instructed in the command prompt
mvn --version
the response was
Unrecognized option: -
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Java also is unable to run some .java .class and .jar file (which is normal, but before I had managed to make at least one of them work).

The instruction
java --version
returns
openjdk 18.0.2.1 2022-08-18
OpenJDK Runtime Environment (build 18.0.2.1+1-1)
OpenJDK 64-Bit Server VM (build 18.0.2.1+1-1, mixed mode, sharing)

I looked for a solution on helpdeskgeek.com/help-desk/how-to-fix-could-not-create-the-java-virtual-machine-error/
None of the suggestions work.

Uninstalling Java fails because it is not listed in Windows Apps list. Java’s installation folder also doesn’t seem to contain an uninstaller.

I ‘installed’ Java this way: I downloaded the file openjdk-18.0.2.1_windows-x64_bin.zip from somowhere and unzipped it to its final location. I suspect that before doing that I uninstalled Java using the Windows App list. (I remember that not having to run an installer confused me.) There is often something there, as in the HelpDeskGeek article, called Java 8. what is Java 8 ?

How can I get Maven to work and what is the difference between extracting Java and doing a Windows installation ?
 
Saloon Keeper
Posts: 15732
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us which version of Maven you're using.

In my version of Maven (3.8.5), both -version and --version work and return the same output.

I'm pretty sure there were versions of the java command that only accepted -version and not --version, so maybe the same is true for Maven.

Try "mvn -version" with a single hyphen.
 
Marnix Gheyssen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using version 3.8.6.

Instructing
mvn -version
yields
Unrecognized option: -
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you enter that command? Did you copy paste it, or did you type it? Because if you copy-pasted it, then that - may not be an actual - (\u002D) but a unicode character that very much looks like a -.
 
Saloon Keeper
Posts: 28328
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maven itself is a Java app, but I think the "mvn.exe" command is a native wrapper to launch the JVM that Maven will execute under. So I'll consider that likely a suitable JVM cannot be located. This will most likely be because an executable java cannot be located on the PATH and no valid JAVA_HOME is defined.

Note that when defining a PATH for java, you point the the "bin" directory of the desired JVM installation. When you define a JAVA_HOME, however, it points to the parent directory of the java bin directory, which is to say the root of the JDK directory subtree.

You also need to have M2_HOME defined, since that's where Maven keeps its config and its repository cache.
 
Marnix Gheyssen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Rob Spoor :
I typed “mvn -version”.

Java and Maven worked without the environment system variables M2, JAVA_OPTIONS and MAVEN_OPTS.
Now these variables are :
JAVA_HOME = C:\Program Files\Java\jdk-18.0.2.1
JAVA_OPTIONS = -Xmx512M
M2 = %MAVEN_HOME%\bin
MAVEN_HOME = C:\Dev\apache-maven-3.8.6
MAVEN_OPTS = -Xms256m – Xmx512m
PATH = %JAVA_HOME%\bin;%MAVEN_HOME%\bin;TRICENTIS_SEARCH%;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;;C:\Program Files\Dev\MS Visual Studio Code\bin

Java is located in C:\Program Files\Java\jdk-18.0.2.1
Maven is located in C:\Dev\apache-maven-3.8.6
 
Stephan van Hulst
Saloon Keeper
Posts: 15732
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's the current output of "mvn -version"? Still nothing?
 
Rancher
Posts: 5035
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I enter PATH in a command prompt, I do NOT get any % characters.   All the variables have been resolved when the path is displayed.

There appears to be an unpaired % here:  ;TRICENTIS_SEARCH%;
 
Stephan van Hulst
Saloon Keeper
Posts: 15732
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a great catch Norm.
 
Tim Holloway
Saloon Keeper
Posts: 28328
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, "M2" is defined in terms of MAVEN_HOME, but MAVEN_HOME isn't defined yet!

FYI: Java itself uses no environment variables at all. Maven does use some, but I'd have to read the docs to get the exact set. Definitely M2_HOME and JAVA_HOME, though.

I would not be surprised to learn that we have yet another case of spaces-in-filenames here, though.

Type "java -version" and see what happens.
 
Marnix Gheyssen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I in the Command Prompt instruct
 mvn -version
I still get
 Unrecognized option: -
 Error: Could not create the Java Virtual Machine.
 Error: A fatal exception has occurred. Program will exit.

When I in the Command Prompt instruct
 PATH
I don’t get any % either, except the one after TRICENTIS_SEARCH.

In my previous post I have copied the PATH variable from the System Environment Variables interface. (I don’t get the fancy table anymore.)

Tim Holloway: “Also, "M2" is defined in terms of MAVEN_HOME, but MAVEN_HOME isn't defined yet!”
The variables are listed in alphabetical order.

Tim Holloway: “FYI: Java itself uses no environment variables at all. Maven does use some, but I'd have to read the docs to get the exact set. Definitely M2_HOME and JAVA_HOME, though.”
If I remove the path to Java from PATH, then Java doesn’t work anymore.

Instructing
 java -version
yields
 openjdk version "18.0.2.1" 2022-08-18
 OpenJDK Runtime Environment (build 18.0.2.1+1-1)
 OpenJDK 64-Bit Server VM (build 18.0.2.1+1-1, mixed mode, sharing)

From PATH I remove one of doubled semicolon and add a % in front of TRICENTIS_SEARCH.

Now I instruct
 PATH
and get
 PATH=C:\Program Files\Java\jdk-18.0.2.1\bin;C:\Dev\apache-maven-3.8.6\bin;C:\Program Files (x86)\TRICENTIS\Tosca Testsuite\ToscaCommander;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;C:\Program Files\Dev\MS Visual Studio Code\bin;C:\Users\Knarf\AppData\Local\Microsoft\WindowsApps;C:\Users\Knarf\.dotnet\tools

The TRICENTIS_SEARCH variable displays with an extra semicolon for some reason (see after ToscaCommander).

Mvn -version still doesn’t work.
 
Stephan van Hulst
Saloon Keeper
Posts: 15732
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post the output of running the following commands?

  • echo %JAVA_HOME%
  • echo %MAVEN_HOME%
  • echo %M2%

  • Normally we're not fans of screenshots, but can you make screenshots of the following:

  • The Windows command prompt after you've executed "echo %PATH%".
  • The Windows command prompt after you've executed "mvn -version".
  • The Windows command prompt after you've executed "mvn".
  • The contents of the C:\Dev\apache-maven-3.8.6\bin folder.
  •  
    Tim Holloway
    Saloon Keeper
    Posts: 28328
    210
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In Windows, PATH isn't an enviroment variable as such, unlike Unix. But PATH isn't used by Java in any case, it's used by the command shell. You can leave C:\Program Files\Java\jdk-18.0.2.1 out of the PATH, type "C:\Program Files\Java\jdk-18.0.2.1\bin\java.exe" at the C: prompt and still get the same result. Please note that the quotes are essential thanks to the space in "Program Files".

    The following should suffice:


    Everything else is just gravy. Note that I did quote JAVA_HOME (spaces in path, again). Anyone who's in the know, feel free to correct me, as I don't run Windows. I don't think you need to quote the path add argument for adding JAVA_HOME\bin, but have no way to check. I mentioned M2_HOME, but I think that's garbled or optional (assumed DOS version of $HOME/.m2).
     
    Marnix Gheyssen
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Since inserting the image failed, here is its content :





    It used to be like that and to work in the beginning (except for the “” around the JAVA_HOME path), but I and programs have been meddling with it since then. I have been meddling with it because :
    1) I was told to by instructions I was trying to follow,
    2) to try and fix things when it didn’t work,
    3) to bring order to the mess on my computer.

    No one has yet commented on the Windows installation aspect of this issue. What is Java 8 ?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15732
    368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your output suggests to me that your Maven installation is corrupted somehow. I think there might be an error in one of the batch scripts that Maven consists of.

    Try removing your Maven installation completely, then download it anew and extract the archive to the same location you had it before.
     
    Tim Holloway
    Saloon Keeper
    Posts: 28328
    210
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Java 8 is Version 8 of the JDK/JRE products. It's pretty much obsolete and the oldest version you should consider now is Java 9.

    But the JVM in your examples is Java 18, which is much newer than that. And that's OK.

    The Maven version you're using is 3.8, and that also is OK.

    I'm beginning to side with Stephan. You have an error that says "Unrecognized option: -" and there is no legitimate way that that should be an error to Maven unless you used a word processor to create the command and it used an "n-dash" character instead of vanilla dash. Yes, it's very hazardous top copy-and-paste commands and code straight out of text documents. They often have things in them that look like basic characters but are actually something different. The curly quotes, for example. Virtually no programming language environment likes them, but they're proper for use in something like “Pride and Prejudice”.

    But when all else fails, delete the entire Maven 3.8 directory and its children and unzip a fresh copy in its place.
     
    Marnix Gheyssen
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I delete everything from C:\Dev\apache-maven-3.8.6 and Instruct
     mvn -version
    Response :
     'mvn' is not recognized as an internal or external command,
     operable program or batch file.

    I download Maven again, put the extracted content in that folder and restart the Command Prompt.
    I instruct
     mvn
    Response:
     Unrecognized option: -
     Error: Could not create the Java Virtual Machine.
     Error: A fatal exception has occurred. Program will exit.

    Notice that no option was provided.
     
    Norm Radder
    Rancher
    Posts: 5035
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A couple of tests to try.  Enter these commands in a command prompt:
    mvnDebug

    mvn.cmd

    Copy the command prompt window's contents and paste it here
     
    Marnix Gheyssen
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Tim Holloway :”Java 8 is Version 8 of the JDK/JRE products. It's pretty much obsolete and the oldest version you should consider now is Java 9.”
    So, Java 8 is JDK 8.
    What is the difference between a Windows install of Java (as HelpDeskGeek assumed) and a mere installation by putting it in some folder and setting the path ?

    From the Command Prompt :

    C:\Users\Knarf>mvnDebug
     Unrecognized option: -
     Error: Could not create the Java Virtual Machine.
     Error: A fatal exception has occurred. Program will exit.

    C:\Users\Knarf>mvn.cmd
     Unrecognized option: -
     Error: Could not create the Java Virtual Machine.
     Error: A fatal exception has occurred. Program will exit.

    C:\Users\Knarf>

    I delete the variable M2, JAVA_OPTIONS and MAVEN_OPTS from System Variables (and restart the Command Prompt).

    I instruct
     mvn -version
    Response :
     C:\Users\Knarf>mvn -version
     Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
     Maven home: C:\Dev\apache-maven-3.8.6
     Java version: 18.0.2.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-18.0.2.1
     Default locale: nl_BE, platform encoding: UTF-8
     OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

    I add M2 = %MAVEN_HOME%\bin
    No problem

    I add JAVA_OPTIONS = -Xmx512M
    No problem

    I add MAVEN_OPTS = -Xms256m – Xmx512m
    Response to mvn -version
     Error: Could not find or load main class ?
     Caused by: java.lang.ClassNotFoundException: ?

    (That is a different error than before).
    (Exercise : find the typo. Solution : see tutorialspoint article. It also explain the “Unrecognized option: -” error message.)
    Correcting the typo solves the problem.

    I delete the M2 and JAVA_OPTIONS variables and “mvn -version” it still works.

    Java and/or Maven should make their error messages more instructive.

    Thanks for your help guys. I am confident that further in the tutorial there is something else that won’t work.
     
    Tim Holloway
    Saloon Keeper
    Posts: 28328
    210
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Although Java can be bundled in an OS-specific installer package, the JDK itself is just a ZIP archive. So while you can run an OS installer, it's also OK to simply download and unZIP anywhere you want to. This is important in part because it allows you to have more than one version of Java running on your machine.

    When you set JAVA_HOME, you set it to the path of top-level directory that was unzipped. That's all.

    Same idea with Maven, although I don't think that there are OS-specific installers for Maven (except maybe for Red Hat Linux).
     
    Marnix Gheyssen
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As to be expected, some other thing isn’t working. I have got till https://www.tutorialspoint.com/maven/maven_build_profiles.
    So far, I have only partly understood the tutorial, but their instructions at least worked.

    I set my pom as the one provided after “Assume, we've created the following pom.xml in C:\MVN\project folder.”

    Then instruct
    C:\Users\Knarf\Documents\School\Informatica\Software Testing\Maven\Maven tutorial\project

    Response : (I have ommitted all the download messages)
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ----------------< com.companyname.projectgroup:project >----------------
    [INFO] Building project 1.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\Users\Knarf\Documents\School\Informatica\Software Testing\Maven\Maven tutorial\project\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
    [INFO] No sources to compile
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ project ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory C:\Users\Knarf\Documents\School\Informatica\Software Testing\Maven\Maven tutorial\project\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ project ---
    [INFO] No sources to compile
    [INFO]
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ project ---
    <a few dozen download messages omitted>
    [INFO] No tests to run.
    [INFO]
    [INFO] --- maven-antrun-plugin:1.1:run (default) @ project ---
    [INFO] Executing tasks
        [echo] Using env.test.properties
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  3.435 s
    [INFO] Finished at: 2022-09-10T14:31:29+02:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.1:run (default) on project project: Error executing ant tasks: Warning: Could not find file C:\Users\Knarf\Documents\School\Informatica\Software Testing\Maven\Maven tutorial\project\src\main\resources\env.test.properties to copy. -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException


    Maven complains it can’t find ...\project\src\main\resources\env.test.properties.
    Indeed, the src folder doesn’t exist. Yet for some reason TutorialsPoint expects that to work. (The tuturial shows a fictional directory structure without instruction to nor explanation on how to create it.) Is that just a very bad tutorial or is there some simple way to make that work that has escaped me ?
     
    Tim Holloway
    Saloon Keeper
    Posts: 28328
    210
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, that's progress. Not sure about the "Maven Ant Plugin", though.

    Maven works by "magic". You specify one or more goals, and it uses internally-defined rules to accomplish them, based on a pre-defined project structure.

    Here are some important directories:

    src/main/java - Your java source code (and packages) go here
    src/main/resources - Your static resources (and packages) go here (they're get copied into the final product)
    src/test/java  - The source code for your tests and packages go here. That way they don't get dumped unto the final product, but can still do testing.
    src/test/resources - Resources used when testing
    target - the output of your Maven build goes here, including the intermediate files created while building. A "mvn clean" deletes this directory and it gets re-built next build. In this case, the goal is named "clean". To compile only, you could do a "mvn compile" and the classes would compile under the target directory.

    And, of course, there's the POM: the XML project definition file that says what kind of product you are building (JAR, WAR, EAR, whatever), what its name is, what its version is, what other JARs it depends on, and so forth.

    Also worth noting that a lot of goals implicitly run sub-goals, so for example "mvn assembly" will have the "compile"  goal executed automatically.
     
    Marnix Gheyssen
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I created the folder structure shown in the tutorial. It still doesn’t work for the same reason. Whoever wrote that tutorial seems to overestimate Maven’s magical abilities. However powerful they may be, Maven still can’t find what doesn’t exist.

    I think I will give up on this tutorial. I have noticed there is another course called ‘Learn Maven’.
     
    Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic