NoClassDefFoundError while running a java program in terminal from IDE - java

I am having trouble running a java program that works fine in the IntelliJ IDEA ide. The error I get when I run the same code (after removing the package ..) as follows
Exception in thread "main" java.lang.NoClassDefFoundError: fcrypt
Caused by: java.lang.ClassNotFoundException: fcrypt
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
All I'm doing in the main method is creating an instance of the main class and calling the several methods. The code with just the headers and the main method as below
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
/**
* Created by Aditya Rao on 05/02/14.
*/
public class fcrypt {
private static final String RSA_NONE_PKCS1PADDING = "RSA/None/PKCS1Padding";
static {
Security.addProvider(new BouncyCastleProvider());
}
....
public static void main (String[] args) throws Exception {
if (args.length != 5) {
System.out.print("Invalid parameters. ");
printUsage();
System.exit(1);
}
if (!(args[0].equals("-e") | args[0].equals("-d"))) {
System.out.print("Please specify usage. ");
printUsage();
System.exit(1);
}
fcrypt f = new fcrypt();
String[] inputs = Arrays.copyOfRange(args, 1, args.length);
if (args[0].equals("-e"))
f.encryptAndSign(inputs);
else
f.verifyAndDecrypt(inputs);
}
}
Am I missing something here?
EDIT I compile and run this program with the following commands
javac -cp libs/bcprov-jdk15on-150.jar fcrypt.java
java -cp libs/bcprov-jdk15on-150.jar fcrypt <args>

You have to add working directory denoted as . to the class path as fcrypt.class is located there.
Syntax for Unix:
java -cp ".:libs/bcprov-jdk15on-150.jar" fcrypt
note elements are separated with :.
Syntax for Windows:
java -cp ".;libs/bcprov-jdk15on-150.jar" fcrypt
note elements are separated with ;.
Java code style suggests class names to start with a capital letter. So it should be class FCrypt defined in FCrypt.java.

Related

How can I solve that type of issue when I compile using `javac`: " error: cannot find symbol [...]"?

For starters, I have to say that I am using IntelliJ IDEA Community Edition 2020.3.1 and running java 15.0.1 2020-10-20, also when I run my program after enabling assertions and clicking on the run button, it works as expected. That being said here is my file structure:
Here is the code in my TestRunner.java file:
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
public final class TestRunner {
private static final List<Class<?>> TESTS = List.of(CalculatorTest.class);
public static void main(String[] args) throws Exception {
List<String> passed = new ArrayList<>();
List<String> failed = new ArrayList<>();
for (Class<?> klass : TESTS) {
if(!UnitTest.class.isAssignableFrom(klass)){
throw new IllegalArgumentException("Class "+ klass + " must implement UnitTest");
}
for(Method method : klass.getDeclaredMethods()){
if(method.getAnnotation(Test.class) != null){
try{
UnitTest test = (UnitTest) klass.getConstructor().newInstance();
test.beforeEachTest();
method.invoke(test);
System.out.println(method.invoke(test));
test.afterEachTest();
passed.add(getTestName(klass, method));
}catch(Throwable throwable){
failed.add(getTestName(klass, method));
}
}
}
}
System.out.println("Passed tests: " + passed);
System.out.println("FAILED tests: " + failed);
}
private static String getTestName(Class<?> klass, Method method) {
return klass.getName() + "#" + method.getName();
}
}
Here are my issues:
When I compile my main class TestRunner.java using javac TestRunner.java, it fails to find those symbols CalculatorTest.class, UnitTest.class, Test.class, UnitTest. Here is the error message:
When I use javac *.java though, my files compile and my .class files are generated, here is a screenshot:
but when I try to run my file using java TestRunner it says: "Error could not find or load class TestRunner, here is a screenshot:
If anyone can help me solve those issues I'd would be very happy. So far, I have found no solutions when I googled about them. Thank you!
After reading many answers, I found that the solution was simple.
First compiling TestRunner.java:
javac -cp . TestRunner.java
Then running TestRunner (containing my main function):
java -cp . -ea TestRunner
It turns out I was missing on the dot "."!
Here is the final result:

Compile Error: Package sqlj.runtime does not exist

I am trying to do a homework assignment in a database course that requires me to use sqlj. It is supposed to make a connection to a SQL database running on my computer. When copying and pasting the example code into Netbeans I get a compile error:
package sqlj.runtime does not exist: import sqlj.runtime.*;
import java.sql.*;
import java.io.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
import oracle.sqlj.runtime.*;
public class A10Q1 {
public static void main(String[] args) {
DefaultContext cntxt = oracle.getConnection("url", "user", "pswd", true);
}
}
I changed the inputs on the getConnection function.

Java program not working using the command line

Short story :
When I run my java application through the Intellij it's all working.
When I run it through the command line I have some issues.
Long story:
First, I have to say that I have a 'lib' folder inside my project with all the Jars I need and I added it as a Library to the project.
When I compile it from the command line I have to specify a '-cp' to the lib folder, otherwise it doesn't load the jars. Even though it looks good, when I run my java application, I get a 'Error: Could not find or load main class awsUpdater' error
My commands :
For compiling -
javac -cp "../../../../lib/*" awsUpdater.java
For executing -
java -cp "../../../../lib/*" awsUpdater
Here's my class (besides the methods)
package AWSUpdater;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class awsUpdater {
public static void main(String[] args) throws IOException {
String bucketName = "bucket";
String key = "ket";
//AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
AmazonS3 s3Client = new AmazonS3Client(DefaultAWSCredentialsProviderChain.getInstance());
System.out.println("Downloading an object");
S3Object s3object = s3Client.getObject(new GetObjectRequest(
bucketName, key));
//Get new version of android
String newAndroidVersion = getNewAndroidVersion();
//Download current versions.json
String currentJson = displayTextInputStream(s3object.getObjectContent());
//Edit versions.json with new android version
String editedJson = editJsonWithCurrentAndroidVersion(currentJson, newAndroidVersion);
//String editedJson = editJsonDummyCheck(currentJson);
//Create new file to upload to S3
createFileWithNewJson(editedJson);
//Upload new file to S3
updateVersion(bucketName, key, "versions.json");
}
Would appreciate any help with how to compile and execute my program through the command line. thanks !
you need to add package name
java -cp "../../../../lib/*" AWSUpdater.awsUpdater
I notice that the class awsUpdater is under the package AWSUpdater, so you can not use java -cp "../../../../lib/*" awsUpdater directly.
For Example:
I create a project like this:
|-test
|-AWSUpdater
|-awsUpdater.java
Detail of the awsUpdater.java:
public class awsUpdater {
public static void main(String[] args) {
System.out.println("hello");
}
}
then(now I'm in test/AWSUpdater):
javac awsUpdater.java
java awsUpdater
Everything goes well!
If I add the class to the package, like this:
package AWSUpdater;
public class awsUpdater {
public static void main(String[] args) {
System.out.println("hello");
}
}
then(now I'm in test/AWSUpdater):
javac awsUpdater.java
java awsUpdater
here, it will got the error which is same with yours.
Now, you can go to the package's root dir. (here is test), and then:
javac AWSUpdater/awsUpdater.java
java AWSUpdater/awsUpdater
Now, you will get the correct result.

Error: Could not find or load main class using external libraries

I am trying to execute a java program I wrote. Doing the compiling is no problem ( I used : javac -cp \* *.java). But when i try to run it (using: java -cp \* FirstTestCase 1 1 data), it says Error: Could not find or load main class FirstTestCase.
this is the code:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Timer;
public class FirstTestCase {
public static void main(String[] args) throws IOException, InterruptedException, CertificateException {
// TODO Auto-generated method stub
int amount = 100000;
int threads = 150;
String file = "newData";
if(args.length == 3)
{
amount = Integer.parseInt(args[0]);
threads = Integer.parseInt(args[1]);
file = args[2];
}
X509Downloader d = new X509Downloader(amount, threads, file);
d.getCertificates();
}
}
We are also using external libraries, 4 to be precise. We use the -cp \* to include them all. When i run java FirstTestCase 1 1 data it succeeds to run it but then throws Exception about it not finding the classes that we use from the external libraries.
I normally use an IDE and therefore i barely have any experience using the terminal to start java programs.
I do use a Mac at the moment to test it, but we will run this program later on a server using Linux (in case the operating system matters)

why i am not able to read Html Content from a website in a file?

I have made a java program where in i can use any website to read its Html Content using Scanner class and Varargs.I am not able to get the output while i am using Scanner class and VarArgs.
Below is the following Code.
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
public class ReadWebsite
{
public static void main(String[] args) throws Exception
{
URL oracle = new URL(args[0]);
Scanner s=new Scanner(oracle.openStream());
while (s.hasNext())
{
System.out.println(s.nextLine());
}
s.close();
}
}
OutputShown
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at oodlesTech.ReadWebsite.main(ReadWebsite.java:15)
if you are running from eclipse you have to pass the arguments.
right click program - > run as - > run configurations -> arguments ->program arguments . in this tab pass the actual url which will be passed as args[0] to your main method.
You are not passing the argument to your java program.
For your testing you can either hard code it in code e.g. URL oracle = new URL("http://www.google.com"); or pass an argument to your java program, explained here

Categories