Use static variable from JAR file












0















I've created a runnable JAR file which has a class with this code:



static KeysetHandle keysetHandle = null;

public String encrypt(String plainText){

Config.register(AeadConfig.TINK_1_1_0);

// GENERATE key
// key generated using tink library
keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES256_GCM);
}

public String decrypt(String cipherText){

//using the key generated during encryption to decrypt
Aead aeadDecryption = AeadFactory.getPrimitive(keysetHandle);

}


In my other Java application I've imported this JAR file as an external JAR file and am trying to run these methods:



import core.Crypto;

public class Sample {

public static void main(String args) {
// TODO Auto-generated method stub
Crypto crypto = new Crypto();
System.out.println(crypto.encrypt("sampleText"));
System.out.println(crypto.decrypt("XXX"));
}
}


The encrypt function works as expected, but the decrypt returns a NullPointerException because keysetHandle is null. How do I get the updated value for keysetHandle?










share|improve this question




















  • 1





    You should provide more details about how keysetHandle is getting initialized? Is it based on a certain condition? Also an observation, why are you depending on initialization of static variable from non-static method? The approach isn't correct.

    – CuriousMind
    Nov 19 '18 at 4:15











  • Added more detail to the code. I'm using static variable so that I can access it from both methods. Is there a better way?

    – sibi chakravarthy
    Nov 19 '18 at 16:44











  • It doesn't have to be static. You can still access it from both methods.

    – DodgyCodeException
    Nov 19 '18 at 16:50











  • You should have a look at generateNew method and figure out under what circumstances it can return a null value. That will provide a clue about your problem.

    – CuriousMind
    Nov 20 '18 at 4:27











  • @CuriousMind github.com/google/tink/blob/master/java/src/main/java/com/… shows that generateNew never returns null. It's more likely another thread is setting the static field to null.

    – DodgyCodeException
    Nov 20 '18 at 9:40
















0















I've created a runnable JAR file which has a class with this code:



static KeysetHandle keysetHandle = null;

public String encrypt(String plainText){

Config.register(AeadConfig.TINK_1_1_0);

// GENERATE key
// key generated using tink library
keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES256_GCM);
}

public String decrypt(String cipherText){

//using the key generated during encryption to decrypt
Aead aeadDecryption = AeadFactory.getPrimitive(keysetHandle);

}


In my other Java application I've imported this JAR file as an external JAR file and am trying to run these methods:



import core.Crypto;

public class Sample {

public static void main(String args) {
// TODO Auto-generated method stub
Crypto crypto = new Crypto();
System.out.println(crypto.encrypt("sampleText"));
System.out.println(crypto.decrypt("XXX"));
}
}


The encrypt function works as expected, but the decrypt returns a NullPointerException because keysetHandle is null. How do I get the updated value for keysetHandle?










share|improve this question




















  • 1





    You should provide more details about how keysetHandle is getting initialized? Is it based on a certain condition? Also an observation, why are you depending on initialization of static variable from non-static method? The approach isn't correct.

    – CuriousMind
    Nov 19 '18 at 4:15











  • Added more detail to the code. I'm using static variable so that I can access it from both methods. Is there a better way?

    – sibi chakravarthy
    Nov 19 '18 at 16:44











  • It doesn't have to be static. You can still access it from both methods.

    – DodgyCodeException
    Nov 19 '18 at 16:50











  • You should have a look at generateNew method and figure out under what circumstances it can return a null value. That will provide a clue about your problem.

    – CuriousMind
    Nov 20 '18 at 4:27











  • @CuriousMind github.com/google/tink/blob/master/java/src/main/java/com/… shows that generateNew never returns null. It's more likely another thread is setting the static field to null.

    – DodgyCodeException
    Nov 20 '18 at 9:40














0












0








0








I've created a runnable JAR file which has a class with this code:



static KeysetHandle keysetHandle = null;

public String encrypt(String plainText){

Config.register(AeadConfig.TINK_1_1_0);

// GENERATE key
// key generated using tink library
keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES256_GCM);
}

public String decrypt(String cipherText){

//using the key generated during encryption to decrypt
Aead aeadDecryption = AeadFactory.getPrimitive(keysetHandle);

}


In my other Java application I've imported this JAR file as an external JAR file and am trying to run these methods:



import core.Crypto;

public class Sample {

public static void main(String args) {
// TODO Auto-generated method stub
Crypto crypto = new Crypto();
System.out.println(crypto.encrypt("sampleText"));
System.out.println(crypto.decrypt("XXX"));
}
}


The encrypt function works as expected, but the decrypt returns a NullPointerException because keysetHandle is null. How do I get the updated value for keysetHandle?










share|improve this question
















I've created a runnable JAR file which has a class with this code:



static KeysetHandle keysetHandle = null;

public String encrypt(String plainText){

Config.register(AeadConfig.TINK_1_1_0);

// GENERATE key
// key generated using tink library
keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES256_GCM);
}

public String decrypt(String cipherText){

//using the key generated during encryption to decrypt
Aead aeadDecryption = AeadFactory.getPrimitive(keysetHandle);

}


In my other Java application I've imported this JAR file as an external JAR file and am trying to run these methods:



import core.Crypto;

public class Sample {

public static void main(String args) {
// TODO Auto-generated method stub
Crypto crypto = new Crypto();
System.out.println(crypto.encrypt("sampleText"));
System.out.println(crypto.decrypt("XXX"));
}
}


The encrypt function works as expected, but the decrypt returns a NullPointerException because keysetHandle is null. How do I get the updated value for keysetHandle?







java tink






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:39







sibi chakravarthy

















asked Nov 19 '18 at 3:24









sibi chakravarthysibi chakravarthy

134




134








  • 1





    You should provide more details about how keysetHandle is getting initialized? Is it based on a certain condition? Also an observation, why are you depending on initialization of static variable from non-static method? The approach isn't correct.

    – CuriousMind
    Nov 19 '18 at 4:15











  • Added more detail to the code. I'm using static variable so that I can access it from both methods. Is there a better way?

    – sibi chakravarthy
    Nov 19 '18 at 16:44











  • It doesn't have to be static. You can still access it from both methods.

    – DodgyCodeException
    Nov 19 '18 at 16:50











  • You should have a look at generateNew method and figure out under what circumstances it can return a null value. That will provide a clue about your problem.

    – CuriousMind
    Nov 20 '18 at 4:27











  • @CuriousMind github.com/google/tink/blob/master/java/src/main/java/com/… shows that generateNew never returns null. It's more likely another thread is setting the static field to null.

    – DodgyCodeException
    Nov 20 '18 at 9:40














  • 1





    You should provide more details about how keysetHandle is getting initialized? Is it based on a certain condition? Also an observation, why are you depending on initialization of static variable from non-static method? The approach isn't correct.

    – CuriousMind
    Nov 19 '18 at 4:15











  • Added more detail to the code. I'm using static variable so that I can access it from both methods. Is there a better way?

    – sibi chakravarthy
    Nov 19 '18 at 16:44











  • It doesn't have to be static. You can still access it from both methods.

    – DodgyCodeException
    Nov 19 '18 at 16:50











  • You should have a look at generateNew method and figure out under what circumstances it can return a null value. That will provide a clue about your problem.

    – CuriousMind
    Nov 20 '18 at 4:27











  • @CuriousMind github.com/google/tink/blob/master/java/src/main/java/com/… shows that generateNew never returns null. It's more likely another thread is setting the static field to null.

    – DodgyCodeException
    Nov 20 '18 at 9:40








1




1





You should provide more details about how keysetHandle is getting initialized? Is it based on a certain condition? Also an observation, why are you depending on initialization of static variable from non-static method? The approach isn't correct.

– CuriousMind
Nov 19 '18 at 4:15





You should provide more details about how keysetHandle is getting initialized? Is it based on a certain condition? Also an observation, why are you depending on initialization of static variable from non-static method? The approach isn't correct.

– CuriousMind
Nov 19 '18 at 4:15













Added more detail to the code. I'm using static variable so that I can access it from both methods. Is there a better way?

– sibi chakravarthy
Nov 19 '18 at 16:44





Added more detail to the code. I'm using static variable so that I can access it from both methods. Is there a better way?

– sibi chakravarthy
Nov 19 '18 at 16:44













It doesn't have to be static. You can still access it from both methods.

– DodgyCodeException
Nov 19 '18 at 16:50





It doesn't have to be static. You can still access it from both methods.

– DodgyCodeException
Nov 19 '18 at 16:50













You should have a look at generateNew method and figure out under what circumstances it can return a null value. That will provide a clue about your problem.

– CuriousMind
Nov 20 '18 at 4:27





You should have a look at generateNew method and figure out under what circumstances it can return a null value. That will provide a clue about your problem.

– CuriousMind
Nov 20 '18 at 4:27













@CuriousMind github.com/google/tink/blob/master/java/src/main/java/com/… shows that generateNew never returns null. It's more likely another thread is setting the static field to null.

– DodgyCodeException
Nov 20 '18 at 9:40





@CuriousMind github.com/google/tink/blob/master/java/src/main/java/com/… shows that generateNew never returns null. It's more likely another thread is setting the static field to null.

– DodgyCodeException
Nov 20 '18 at 9:40












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367910%2fuse-static-variable-from-jar-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367910%2fuse-static-variable-from-jar-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word