1

I am trying to encrypt a simple string using jasypt. It is working correctly when I use eclipse IDE but has some problem when I try through the terminal.

Output through Eclipse IDE Screenshot

Below is the code which I use.

package com.jasypt.encryption.demo;
import org.jasypt.util.text.BasicTextEncryptor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class BasicDemo {

    public static void main(String[] args) throws IOException {
        String secretkey = "home@123";
        String message = "This is a confidential message. Be Careful !!";
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(secretkey);
        String encrMess = basicTextEncryptor.encrypt(message);
        System.out.println(encrMess);
        String decrMess =basicTextEncryptor.decrypt(encrMess);
        System.out.println(decrMess);
   }
}

I navigate to the folder which contains pom.xml file and enter following commands in terminal

1) mvn package
2) mvn install
3) java -cp target/demo-0.0.1-SNAPSHOT.jar com.jasypt.encryption.demo.BasicDemo

I get BUILD SUCCESS message and jar file is successfully created but I get some error when I run 3rd command. Error Screenshot

Please excuse and suggest something if I am making some very basic mistake or using redundant lines of code as I am new to java.

Osho Anand
  • 13
  • 5

1 Answers1

0

Welcome to StackOverflow!

When you compile your program with Maven (which is actually not a compiler but a package manager that can also call the Java compiler behind the scenes) Maven takes care of downloading and managing the dependencies that your program uses, in this case it is Jasypt.

When you then try to start the program with plain java the information about the dependecies that are necessary to run your program is lost, just because Maven is no longer part of the game. Therefore you have to give the Java runtime a hint where to find the Jasypt dependency, just as you did with your demo-jar. During the compilation process Maven stored the Jasypt jar on your drive, in a folder called local Maven repository.

You now can simply add the path to this jar to your classpath and everything will run:

java -cp target/demo-0.0.1-SNAPSHOT.jar:<path to your Maven repository>/org/jasypt/jasypt/1.9.3/jasypt-1.9.3.jar com.jasypt.encryption.demo.BasicDemo

(The version of the Jasypt library may differ on your machine.)

If you have a lot of dependencies it will become cumbersome to add them all manually to the classpath. Maven can also take care for you of this task, with the help of the Exec-plugin. Instead of starting java directly let Maven do the plumbing for you:

mvn exec:java -Dexec.mainClass="com.jasypt.encryption.demo.BasicDemo"

You could also check this thread for more details about this plugin and its options

werner
  • 13,518
  • 6
  • 30
  • 45
  • I think you diagnosed the error correctly but am not sure about the solution as I haven't checked it. What I did was, add few lines of code in pom.xml to create the jar file with all dependencies including jasypt and then ran and it worked perfectly fine. `java -cp target/demo-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.jasypt.encryption.demo.BasicDemo` – Osho Anand Apr 25 '20 at 07:55
  • I guess the "few lines" were the [Maven Assembly Plugin](https://maven.apache.org/plugins/maven-assembly-plugin/usage.html), correct? This is also an option, with this way you simply include all required classes in your jar. On the plus side you end up with only one large jar and you don't have to care about the classpath anymore. But the assembly plugin also has its own [challenges](https://blog.soebes.de/blog/2013/09/28/build-smells-maven-assembly-plugin/). – werner May 23 '20 at 17:45