Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: Running a jar in a batch file

  1. #1
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Running a jar in a batch file

    Okay, I have a simple question. Currently I have a hello world program in java that I exported to a runnable jar and created a batch file that executes this jar.

    I got an error message originally stating that it could not find the main class. I realized that this was due to the fact that my eclipse was under a different jre than my path was set to. So effectively I was trying to run a jar file that was packaged with java 7, with a JAVA_HOME that was set to java 6.

    My question is, is it possible to create a batch file that will navigate my directory to the java 7 that I have installed and then run the jar file that is in a separate directory? Or does the JAR HAVE to be in the java 7 bin directory first..?? this might fall under DOS question but I didn't know where to go.


    any advice?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Running a jar in a batch file

    In your batch file you can set your JAVA_HOME to whatever you want.

    In any case, not finding the main class has nothing to do with the Java versions...that would be a different error.
    What command are you running?
    Does that command work from the command line?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: Running a jar in a batch file

    sorry right, it wasn't a missing class error it was unsupportedClassVersionError 51


    I have my batch set up like this , to change the java_home variable for this one script then call my jar file, but I still get the unsupported error, and it looks like the JAVA_home variable isn't changing at all, although it says successfully changed In the console window while the batch runs.

    Java Code:
    SETX JAVA_HOME "C:/Program Files/Java/jre7"
    
    java -jar helloworld.jar
    
    pause
    Last edited by scarecrowkid; 01-05-2017 at 09:34 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Running a jar in a batch file

    When Windows looks for an executable/command it uses the PATH variable.
    JAVA_HOME is used by some Java apps (Apache Tomcat I think for example), but not Windows itself.

    Why not correct the PATH if you should be using 7 and not 6?
    Remove the PATH to 6 and replace with the one to 7.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: Running a jar in a batch file

    I have set my java_home to point to 7 and then put the java_home in my path variable however it is still not being reflected over when I run java -version

  6. #6
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: Running a jar in a batch file

    If the PATH variable gets set before your JAVA_HOME variable is set, it's still empty. Better hard code it and try again.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: Running a jar in a batch file

    As far as my problem, I fixed it by setting the java_home in the beginning (instead of middle) of the path variable.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Running a jar in a batch file

    That's because your PATH already had Java 6 on it, presumably earlier int he PATH than where you placed JAVA_HOME.
    That's why I said to remove 6, and then add 7.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    scarecrowkid is offline Member
    Join Date
    Dec 2016
    Posts
    20
    Rep Power
    0

    Default Re: Running a jar in a batch file

    This is what my path looked like before I put JAVA_HOME at the beginning, where else am I supposed to remove java 6 at :/

    I don't see a java at all in here, that's why I was so confused as to why nothing was reflecting, until I put it at the beginning.

    %JAVA_HOME%\bin;C:\app\u129348\product\11.2.0\dbho me_2\bin;%SystemRoot%\system32;%SystemRoot%;%Syste mRoot%\System32\Wbem;%SYSTEMROOT%\System32\Windows PowerShell\v1.0\;
    C:\WINDOWS\CCM;C:\Program Files (x86)\Common Files\Ulead Systems\MPEG;C:\PROGRA~2\IBM\SQLLIB\BIN;C:\PROGRA~ 2\IBM\SQLLIB\FUNCTION;C:\PROGRA~2\IBM\SQLLIB\SAMPL ES\REPL;
    C:\Program Files (x86)\IBM\Personal Communications\;C:\Program Files (x86)\IBM\Trace Facility\;C:\Program Files (x86)\GitExtensions\;ANT_HOME\bin;
    C:\Program Files\Common Files\Hitachi ID\;C:\Program Files (x86)\Common Files\Hitachi ID\;C:\Program Files (x86)\WebEx\Productivity Tools;C:\Program Files\1E\NomadBranch\;
    C:\Program Files (x86)\Skype\Phone\

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Running a jar in a batch file

    Hmmm.
    That begs the question of where exactly your command line was finding java 6 from...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,239
    Rep Power
    23

    Default Re: Running a jar in a batch file

    That's better.
    Moved out the threadjack posts.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Weird problem running a batch file
    By d3n1s in forum Advanced Java
    Replies: 7
    Last Post: 06-19-2011, 01:34 AM
  2. Help with .bat batch file problem
    By Java_Mad in forum New To Java
    Replies: 2
    Last Post: 11-03-2010, 02:52 PM
  3. Replies: 1
    Last Post: 01-16-2009, 11:14 AM
  4. Running batch files in java
    By pradeesh in forum New To Java
    Replies: 10
    Last Post: 12-04-2008, 10:26 PM
  5. Problem in batch file
    By marwa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 10:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •