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.
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
Related
I have found many tutorials, which showed me how to connect with websites and fit some forms, but almost all of them contained small piece of code - without imports, I figured out that i need to use special library like htmlUnit or Watij, so I downloaded it, and tried to use but i couldn't. I have tried putting all jar files from downloaded zip archive to lib folder in java compile but it didnt work. I have also try to compile with
-classpath - but it didn't work - probably I used it in wrong way, all time i m getting .
Main.java:5: error: package [...] does not exist.
How can I add library to java compiler or how can I compile file with library properly.
I m using windows and jdk version: 10.0.1, also i m not using any advanced java editor like eclipse.
So in conslusion:
import gargoylesoftware.htmlunit.WebClient;
import gargoylesoftware.htmlunit.html.HtmlElement;
import gargoylesoftware.htmlunit.html.HtmlOption;
import gargoylesoftware.htmlunit.html.HtmlPage;
import gargoylesoftware.htmlunit.html.HtmlSelect;
final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient.getPage("http://some_url");
I have got something like this in my code and i m trying to compile it (after putting jar files from zip(https://sourceforge.net/projects/htmlunit/) in lib folder (located in jdk folder)) with this command:
javac Main.java
and this(where zipo.zip is downloaded archive from upper link):
javac -classpath watij.jar Downloading.java
but for every import it ended in this compilation error:
Main.java:6: error: package gargoylesoftware.htmlunit.html does not exis
Thanks
I know that it was hard to understand my problem.
I managed to find a solution while trying to paraphrase the problem.
The thing, which i was doing in wrong way is exactly the same as in article below.
How to compile Java program with .jar library
I want to be able to use the Apache Commons Math Library in Java but I cannot get it to work correctly and the main site is frustratingly unhelpful (at least for a novice like me) and I haven't been able to find a solution on here yet.
I went to http://commons.apache.org/proper/commons-math/download_math.cgi
downloaded the first option commons-math3-3.6.1-bin.tar.gz
unzipped it and put it into the folder with the java class that I am trying to build.
I then did the command import org.apache.commons.math3;
But I get Error: package org.apache.commons does not exist
Could someone explain (preferably in detail that not even a novice would misunderstand) why this isn't working and what I should do?
Thanks!
First you need to download jar from repository
https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1
put it to folder test, in same folder create file Test.java with class Test something like this
import org.apache.commons.math3.analysis.function.Abs;
class Test {
public static void main (String ... args) {
Abs abs = new Abs();
System.out.println(abs.value(-10.0d));
}
}
after that compile it with command javac -cp "commons-math3-3.6.1.jar" Test.java
and run it java -cp ".;commons-math3-3.6.1.jar" Test
output will be 10.0
So I was searching for a command line tool to de-compile .jar files, I found this "jd-cmd" at https://github.com/kwart/jd-cmd and it worked fine, but now I saw it has an api that I can use, I can't seem to do it the wright way. I am working on intellij IDE, added the external jars to my project, all I want is that the output will be at a folder of my choice in the filesystem just like it works at the command line with no problems.
here's an example code I tried from the idea that didn't do the job:
import java.io.File;
import jd.core.input.JDInput;
import jd.core.input.ZipFileInput;
import jd.core.output.DirOutput;
import jd.core.output.JDOutput;
import jd.ide.intellij.JavaDecompiler;
public class App {
public static void main(String[] args) {
JavaDecompiler javaDecompiler = new JavaDecompiler();
//choose input plugin for your decompiled file (class, zip, directory)
JDInput jdIn = new ZipFileInput("path/to/myFavorityLib.jar");
//choose output plugin (zip, directory, console)
JDOutput jdOut = new DirOutput(new File("/tmp/decompiled"));
//decompile
jdIn.decompile(javaDecompiler, jdOut);
}
}
This code didnt generate decompiled files at "/tmp/decompiled"
ok, so i have managed to do it .
it was a dependency problem , the github developer mentioned that jd-lib folder should be added as a dependency library , but actually it dosent matter that much ,you need to add a jar file :after doing all the instructions and downloding what ever maven will give you, at the folder jd-cli there a downloaded jar "jd-cli.jar" , use this as a dependency for your project and it work just fine!
I am nearly complete with my internship work. One thing that is holding me back is compiling
I have a directory with three sub directories: lib, source, class. Within the source sub directory I have a GUI.
I try to run the following command to compile the GUI.
javac -cp .:lib\poi-3.11\poi-3.11-20141221.jar -d class\ .\src\GUI\*
Contents of some files withing the GUI are dependent upon Apache POI. When I run this command I get the following error:
src\GUI\CELL_TO_STRING.java:4: error: package org.apache.poi.ss.usermodel does not exist
import org.apache.poi.ss.usermodel.Cell;
Here is the imports of my java file CELL_TO_STRING:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
There are a few more but for brevity, I'll just list these. I believe this is some issue with specifying the class path to the Apache POI. Apache POI is a pretty big library with tons of jar files. So it will be hard to show you what exactly is in it. But if you could download for yourself to help out, that would be awesome.
From what I have seen on the web is "use maven", "use ant", "use this IDE". IDEs/Build Tools are very useful, I understand that. But I want to know how to properly build java programs through a terminal like ms-dos or shell. Having said that, please don't reply with the mentioned responses on other threads.
Please let me know if I need to give some more information.
Check the components page of Apache POI. Could you try after including poi-ooxml and poi-ooxml-shemas jars? These are required for ss it seems.
Here is my JUnit test class:
package com.bynarystudio.tests.storefront;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.bynarystudio.tests.BaseWebTest;
public class SmokeTests extends BaseWebTest {
#Test
public void StoreFrontMegaNavTest(){
webDriver.get(default_homepage);
String source = webDriver.getPageSource();
Assert.assertTrue(source.contains("some text"));
}
}
How do I run this test from the command line? When I try to run it from inside its directory using
java -cp junit.textui.TestRunner SmokeTests.java
I get the following error
Could not find the main class: SmokeTests.java. Program will exit.
I think this has to do with the fact that my classpath is not setup properly. But I have no idea because I'm brand new to Java. Coming from .NET, C#, and Visual Studio, the whole classpath thing makes no sense. Even though I have all my files correctly added to a project in Eclipse (I know because the test runs fine from inside Eclipse), it will absolutely not run or compile from the command line.
First of all you are mixing two things:
First you have to compile the project using javac command. As a result you will get set of .class files (not .java -> this is source code)
Then you can run the code using java command:
java -cp classPath yourpackage.SmokeTests
where:
classPath - is the list of directories or jar files where your compiled classes are, if you jave multiple entries separate them using ";" (windows) or ":"(Linux)
so your classPath can be: -cp .;c:/jars/*;deps
which means your classpath will contain:
current directory
all jar files from c:/jars/*
all jar files from deps directory that is in your working dir
So the full command can will be:
java -cp .;c:/jars/*;deps SmokeTests
yourpackage - is the package of the SmokeTests class, if you do not have package defined in the SmokeTests.java leave this empty