I know this is basic stuff, but I couldn't figure it out with Google so here I am. I have a backend that allows for the use of web sockets on my website using jetty/java. When I want the backend to work, I have to cd into the project's directory and then run the command 'mvn jetty:run.' Before that, I have to add maven's bin file to the PATH variable. This is all done on an AWS instance. So what I'm wondering is, how do I have the maven package run on startup of the server (after running mvn package of course). Is there something I need to add to init.d? I'm really not sure.
You need to add an entry point, something like this:
package com.yourapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class YourApp {
public static void main(String[] args) {
SpringApplication.run(YourApp.class,args);
}
}
Related
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
I am pretty much new to apache nifi. Couple of days back I stuck at this problem (which involves miss behaving custom nifi processor). Debugging wasnt helping me well. So I decided to explore nifi mock framework (which I should be doing already, but didnt do it :) as suggested in comments on that question.
I am taking help from these links: 1, 2
What I was trying to do is to send single flow file to custom processor. For that I saved/serialized actual flow file using MergeContent (with FlowFile v3 version) and PutFile processor as suggested here. Now I am trying to re-read this file through code in my test using GetFile processor as follows:
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.apache.nifi.util.TestRunners;
import org.apache.nifi.processors.standard.GetFile;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
public class MyCustomProcessorTest {
#Test
public void testOnTrigger() {
TestRunner runner = TestRunners.newTestRunner(new GetFile());
runner.setProperty(GetFile.DIRECTORY, "C:\\Mahesh\\delete\\serialized-flow-files");
runner.setProperty(GetFile.KEEP_SOURCE_FILE, "true");
runner.run(1);
List<MockFlowFile> results = runner.getFlowFilesForRelationship(GetFile.REL_SUCCESS);
System.out.println("done");
}
}
This is giving me following error:
Which maven dependency should I include to get this class? Also is my approach fine or there is any more preferable approach?
I would double-check if org.apache.nifi:nifi-api: is in the classpath.
If using maven I would run
"mvn dependency:tree" to check.
I have some classes throughout a maven project. I would like to add a main method to some of those classes for basic testing while developing.
I tried declaring the class to run with:
mvn exec:java -Dexec.mainClass="huru.util.Async"
but that command looked in my pom.xml file and it ran some pre-configured setup and started my server up as usual. How can I run a specific file (not my regular main class), but still load up the necessary dependencies?
note that for testing I need most of the dependencies in pom.xml, so I will probably need mvn to run the class that I need to test, I can't run it directly with javac.
update sadly, I may need to create a profile in pom.xml since maven can't seem to do very much from the command line. I don't know much about profiles and since I have none in my pom.xml file right now, I am a bit scared of adding that section.
As suggested in the comments, one solution is to skip putting a main method in the class I want to test, but instead create a junit test...this works at the command line:
mvn -Dtest=AsyncTest test
where my test looks like:
package huru;
import huru.util.Async;
import io.vertx.core.Vertx;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Arrays;
#RunWith(VertxUnitRunner.class)
public class AsyncTest {
#Test
public void test(TestContext tc) {
Async.Parallel(Arrays.asList(
v -> {
v.done(null, null);
}
), (e, results) -> {
});
}
}
I would like to build a java application.
System reported error message:
Error: Could not find or load main class com.autoparts.autoeshop.Application
My controller:
package com.autoparts.autoeshop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
#SpringBootApplication
#EnableJpaAuditing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And I have written JAVA_HOME on system property.
Your environment is not completly correct as the JAVA_HOME should point to the installation directory (jdk1.8.0_161) instead of bin and the PATH should include %JAVA_HOME%/bin.
Your trial to compile with javac has been done from the wrong directory, you need to compile when you are in the java directory (including the relative path to the java file like com/autoparts/autoeshop/Application.java). Maybe your project has a build system prepared in the project directory (autoeshop) like Maven (look for a pom.xml) or Gradle (find a build.gradle)? If so, install the required build tool and run it, it will download all required dependencies and compile all the java files for you which is needed before you can run the application using the created jar file (typically found in a target (Maven) or build (Gradle) folder after the build tool ran.
Anyway, if you struggle with this kind of problems you may consider to start with some basic Java or at least Spring Boot tutorials.
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