How to solve Could not find or load main class? - java

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.

Related

GitLab CI: configure YAML file on java project

I have the MyJava.Java that represents my whole project. In the past, I used to execute MyJava.Java in eclipse and working well, but now I want to make the process much more automated and execute it using the GitLab runner pipeline.
my YAML file is very simple just read the MyJava.Java and execute it.
build:
stage: build
only:
- myBranch
tags:
- MyBuilder
image: openjdk:8-jdk
script: javac MyPath/MyJava.java
MyJava has a code and dependency that connected to other files such as this
package myfolder.generator;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import java.util.List;
import org.eclipse.emf.common.util.URI;
But whenever I execute the file it shows an error of every import/package such as:
error: package com.google.inject does not exist - import com.google.inject.Inject;
Any hint?
The result of executing the MyJava.Javewill generates a jar file that contains the data that I want. I just mentioned this in case you have another way.
In order to build a java artifact (jar or war) using a CI/CD pipeline you must configure your project to use a dependency manager such as Maven, Gradle or Ant. After you configure your project you will be able to define a build script on the YML file to compile all the necessary dependencies into a java artifact.

Encryption of simple string using jasypt through terminal

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

How do I install TestFX?

I'd like to put together a set of jUnit tests under Eclipse (Neon) on Windows to automate the testing of JavaFX GUIs. It seems that TestFX is the bee's knees for this kind of thing, but having looked around the internet quite a bit, I'm still not sure how to install TestFX without using Maven or Gradle.
I am not familiar with Maven and Gradle, and trying to follow the simple instructions to install TestFX via Maven was unsuccessful. This was done under Eclipse Mars, after which my simple GUI program threw up a compile error about not being able to find or load main class and a run-time error that Selection does not contain a main type. (The simple GUI program ran without errors previously.) After this, I downloaded the latest Eclipse Neon and tried to start afresh.
This is what I did:
Download and install Eclipse Neon from eclipse.org.
Create a Java Project called TestProject (execution environment JavaSE-1.8).
Grant access to javafx/** (right-click on project -- properties -- Java Build Path -- Libraries -- JRE System Library -- Access Rules -- add).
Create MyClass with minimal contents:
package test;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyClass extends Application {
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Hello World");
stage.show();
}
}
Copy the file testfx-core-4.0.0-20150226.214553-8.jar from the testfx repository (linked from the Via direct download section of How to use TestFX in your project) into my eclipse project, sitting at the same level as the JRE System Library;
Add the jar file to the build path (right-click on project -- properties -- Java Build Path -- Libraries -- Add JARS) - this automatically created a directory called Referenced Libraries and copied the jar file into it; and
Create a JUnit test (right-click on source folder -- New -- JUnit Test Case), filling in the appropriate package and Class under test fields, and giving the test case a name (MyTest).
Then I tried to extend the class MyTest to use testfx:
class MyTest extends GuiTest {} as advised in this link; and (separately)
class MyTest extends ApplicationTest{} as advised in this link.
Here's the code in the second case:
package test;
import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;
public class MyTest extends ApplicationTest { }
In each case, eclipse complains that the superclass GuiTest / ApplicationTest cannot be resolved to a type.
I suspect that the problem is that I haven't properly installed testfx. Can anyone help?
There are multiple dependencies; the following jar files must be added to the Referenced Libraries (after copying the jar files into the project, right-click on the project -- Properties -- Java Build Path -- Libraries -- Add JARs...).
testfx-core-4.0.4-alpha.jar, source: http://mavensearch.io/repo/org.testfx/testfx-junit/4.0.4-alpha
testfx-junit-4.0.4-alpha.jar, source: http://mavensearch.io/repo/org.testfx/testfx-core/4.0.4-alpha
guava-18.0.jar, source: http://mavensearch.io/repo/com.google.guava/guava/18.0
hamcrest-core-1.3.jar, source: http://mavensearch.io/repo/org.hamcrest/hamcrest-all/1.3
The above is independent of Gradle and Maven. Alternatively, the files can also be pulled using Gradle:
Create a Gradle project in Eclipse.
In the dependencies block in build.gradle, insert the following lines (source):
testCompile "org.testfx:testfx-core:4.0.+"
testCompile "org.testfx:testfx-junit:4.0.+"
Right-click the project -- Gradle -- Refresh Gradle Project
This places the required files into the Project and External Dependencies folder.
Useful tutorial for TestFX 4: https://www.youtube.com/watch?v=NG03nNpSmgU.

How to use maven project as backend?

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);
}
}

Exporting Scala project as jar from Eclipse

I have a Scala project and I would like to export it as a jar.
*1. At first I tried creating a Java class for the project as an entry point
public class JMain {
public static void main(String[] args) {
System.out.println("Java main calling Scala main");
SMain.main(new String[] {""}); //SMain.main is the actual *main*
and this worked fine and dandy when launched from Eclipse, but when I export it as jar it'll give me 18 exceptions or so. I do now know how to replicate then "environment" in which Eclipse manages to launch this and I'm prety sure it relies on the fact that Scala is on my system already - I need a self contained jar with everything packed in there.
*2. My second try consisted of trying what lach suggested here How to deploy a Scala project from Eclipse?
namely:
public class JMain {
public static void main(String[] args) {
System.out.println("Java Main");
List<String> argList = new ArrayList<String>();
argList.add("fully.qualified.ClassName"); //???
for (String s : args) argList.add(s);
scala.tools.nsc.MainGenericRunner.main(argList.toArray(new String[0]));
This time it won't even run from Eclipse, although it gives only 6 or so exceptions starting with the famous NoClassDefFoundError. I have a feeling I'm not getting fully.qualified.ClassName right. *3. If the main Scala class is called "Dis.scala" and is located in package "pack" shouldn't this fully.qualified.ClassName be "pack.Dis"?
I'm using Jre 1.6 and Scala 2.9.2
EDIT: I have included all external imported jars, even scala-library.jar - everything is nice and packed in the jar
P.S. I am not familiar with Ant or Maven or Sbt. I just want my Scala project jared - if possible without getting into hairy things.
Here is what worked for me:
1. Create scala project
2. Create Wrapper java project
3. Add the scala-library.jar to you java project build path.
So you only need the 3rd step in addition since the rest looks similar to what I did. Then you can happily use: java - jar file.jar
EDIT:
How to create a JAR File which contains Scala/Code which can be consumed by another Java Project, using Scala - Eclipse IDE.
Create a new Scala Project and define an object with a main method as entry point.
Now create a new Java Project and add your Scala Project to the new ones buildpath. Additionally add the scala-library.jar to the Java project.
Now create a Wrapper class in the java project which calls your entry point class from the scala lib. Run the wrapper class to create a eclipse run configuration and test if you can call the scala project.
Use the Export->Java->Runnable JAR file, Wizard now on the wrapper project.The eclipse run configuration will be used as entrypoint into the JAR. Depending on your needs you may want to :
extract required libraries into generated JAR
or
Package required libraries into generated JAR
Finally you get a complete packaged JAR which you can use like this:
java - jar wrapped.jar
For me, it was relatively straightforward.
Develop and test the project using the scala IDE (or eclipse for java).
once ready, generate the jar for the project using file -> export method.
for submitting the spark (i was writing something for spark), i just had to mention --class option for specifying the main class for the jar.
hope to help.

Categories