Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Issue running a compiled JAR file locally

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issue running a compiled JAR file locally

    Hello,

    I've the following Java code in my component in which I need to access an IBM MQ using SSL/certificate.
    The required keyStore file(MyKStoreFile.pfx) is present under "src/main/resources" folder in my Java project.


    String myKeyStoreFile = ResourceUtils.getFile("classpath:" + "MyKStoreFile.pfx").getAbsolutePath();//-->line #1
    System.setProperty("javax.net.ssl.keyStore", myKeyStoreFile);
    System.setProperty("javax.net.ssl.keyStorePassword ", myKeyStorePassword);


    1. When I run this code in IntelliJ IDE, it works fine and am able to connect to the queue and send messages.
    2. When the above code is sent to Jenkins for compilation and then pushed to cloud server using Harnes/CloudFoundry, it is working fine as well.
    3. But when I run the compiled JAR file(using java -jar myJar.jar) locally on my windows machine, I get an error on line #1 above, saying:
    "class path resource[MyKStoreFile.pfx] cannot be resolved to absolute file path because it does not reside in the file system."

    Can you please explain this behaviour? Why does it work on the server and not when I manually run the compiled JAR locally?
    And how do I make this work?

    Thank you for your help!

  2. #2
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    117
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Issue running a compiled JAR file locally

    1. When I run this code in IntelliJ IDE, it works fine and am able to connect to the queue and send messages.
    2. When the above code is sent to Jenkins for compilation and then pushed to cloud server using Harnes/CloudFoundry, it is working fine as well.

    It should have gone through right then.

  3. #3
    Junior Member
    Join Date
    Mar 2023
    Location
    Vancouver, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issue running a compiled JAR file locally


  4. #4
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Thumbs up Re: Issue running a compiled JAR file locally

    It seems like the issue lies in how the keyStore file path is being resolved when running the JAR file locally on your Windows machine.

    When you run the code in IntelliJ or through Jenkins on a server, the file path "classpath:MyKStoreFile.pfx" is resolved correctly because it's able to locate the file within the project's classpath. However, when you run the compiled JAR file directly from the command line using "java -jar myJar.jar", the classpath might not be resolved in the same way as in the IDE or Jenkins environment.

    To resolve this issue and ensure that the keyStore file can be located when running the JAR file locally, you can try the following approach:

    1. Update code to load the resource as a stream: Instead of trying to get the absolute path of the file, you can load the resource as a stream directly from the classpath. Modify line #1 as follows:

    ```java
    InputStream inputStream = getClass().getResourceAsStream("/MyKStoreFile.pfx");
    ```

    2. Load the keyStore from the input stream: Once you have the input stream, you can load the keyStore using it:

    ```java
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(inputStream, myKeyStorePassword.toCharArray());
    inputStream.close();
    ```

    3. Set the keyStore using the loaded KeyStore object:

    ```java
    SSLContext sslContext = SSLContext.getInstance("TLS");
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.ge tDefaultAlgorithm());
    keyManagerFactory.init(keyStore, myKeyStorePassword.toCharArray());
    sslContext.init(keyManagerFactory.getKeyManagers() , null, null);
    SSLContext.setDefault(sslContext);
    ```

    By loading the keyStore file as a stream directly from the classpath, you ensure that it can be accessed regardless of the environment where the JAR file is executed. This approach should resolve the issue you're encountering when running the JAR file locally on your Windows machine. If you need further help with Java assignment or any programming tasks, there are resources available online that can provide guidance and support like ProgrammingHomeworkHelp.com.

Similar Threads

  1. Replies: 2
    Last Post: January 11th, 2014, 06:58 AM
  2. Downloading file from website and saving it locally.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 29th, 2013, 08:40 AM
  3. Running .JAR file issue.
    By JosPhantasmE in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 27th, 2013, 07:07 AM
  4. Issue with running DataGimmick program
    By Bentino in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:42 AM
  5. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM

Tags for this Thread