Java can't find the main class - java

My java can't find the main class why.helloworld. and I can't figure out why. This is my code:
package why;
public class Helloworld {
public static void main(String[] args) {
System.out.println("viva");
}
}
Environment variables:
CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
JAVA_HOME:C:\Program Files\Java\jdk1.7.0
Path :%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

Unfortunately you have not sent your command line but I guess that you forgot to write the package name when running your application. Use command line:
java -cp YOUR_CLASSPATH why.Helloworld
run it from your project directory. The YOUR_CLASSPATH should be either . or name of your jar file or path to directory where your classes are. Probably it is classes or bin depending on your project structure.

Related

Getting java.lang.ClassNotFoundException when using package

I have a java file ComPac.java with the below code:
package com;
public class ComPac{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The file is located at the path : /home/ec2-user/java_c
To compile this file, I ran javac Compac.java, and the class file was generated.
Now its turn to run the class file.
So I did java ComPac(screenshot below)
Understandably, I got the error Error: Could not find or load main class ComPac. Caused by: java.lang.NoClassDefFoundError: com/ComPac (wrong name: ComPac).
This I am assuming is because the java file has the package com declared in it.
So instead I tried, java com.ComPac and expected it to work(screenshot below).
But I got the error: Error: Could not find or load main class com.ComPac. Caused by: java.lang.ClassNotFoundException: com.ComPac.
So how do I run this? and what exactly is the logic for running when it comes to packages in java?
Java used- openjdk version "11.0.8" 2020-07-14 LTS(AWS Corretto)
OS used- Amazon Linux 2
put the class in a folder called "com"
in a bash shell it's then:
$ java com/ComPac
(from the folder containing "com", not inside of "com")
If you are using Java 11 then you don't need to first compile java file and then run the class file. You can directly run the java file using just
java .\test.java
test.java
package com;
public class test{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
command:
java .\test.java
Output:
Hello World
The correct way of doing it as follows:
javac -d . ComPac.java
The switch -d . asks the compiler to place generated class files at the current directory as . stands for the current directory. Learn more about the switches by simply using the command javac
Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, com has been created and ComPac.class has been placed inside this directory. In order to execute the class, you can now use the following command:
java com.ComPac

How to run a java class on ubuntu?

If I compile and run the program. I don't have any issues.
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
If I add the line package ch01.sec01; it complies correctly with javac. However when I try to run it using java I get:
Error: Could not find or load main class HelloWorld
I have tried the following.
export CLASSPATH=/usr/lib/jvm/java-9-openjdk-amd64/bin:/usr/lib/jvm/java-1.9.0-openjdk-amd64/bin
That is why when you use a package in your code, that path must be the actual path of your java file (That means that your code is supposed to be in a directory called sec01 which is inside directory ch01).
With that being set, when running code inside a package, you need to include the path in the command. To do so, after you have compiled your code with javac, navigate to the root of the path (outside ch01 directory) and type
java ch01.sec01.HelloWorld
This should work.

How to run StanfordCoreNlpDemo.java

I successfully compiled StanfordCoreNlpDemo by running:
javac -cp "*" StanfordCoreNlpDemo.java
and it compiled successfully. I then tried to run it with:
java -cp "*" StanfordCoreNlpDemo
I then received the following error:
Error: Could not find or load main class StanfordCoreNlpDemo
I realize this is a CLASSPATH issue so I tried to add the path to the folder:
/some/path/stanford-corenlp-full-2016-10-31/*
Nonetheless, I still get the same error. How do I run StanfordCoreNlpDemo.java?
This is not a problem of StanfordCoreNlpDemo program because I ran that code in Netbeans before. The problem seems associated with classpath issue.
Since the StanfordCoreNlpDemo.java file belongs to a package
package package edu.stanford.nlp.pipeline.demo;
public class StanfordCoreNlpDemo {
public static final void main(String[] args) throws IOException {
// code goes here
}
}
Then calling the following results in Error: Could not find or load main class TheClassName.
java -cp . StanfordCoreNlpDemo
It must be called with its fully-qualified name:
java -cp . edu.stanford.nlp.pipeline.demo.StanfordCoreNlpDemo
And this edu.stanford.nlp.pipeline.demo directory must exist in the classpath. In this example, ., meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which edu.stanford.nlp.pipeline.demo exists.
Reference
https://stackoverflow.com/a/29331827/5352399
https://stackoverflow.com/a/18093929/5352399

Error: Could not find or load main class SOAPTester

This should be an easy one I have the following code...
package org.me.test;
public class SOAPTester {
public static void main(String[] args) throws Exception{
}
}
Eclipse compiles the classes and puts them in the bin so I go into the bin folder and I tried...
java -cp . SOAPTester
java -classpath . SOAPTester
I went back a folder and tried...
java -cp ./bin/org/me/test/SOAPTester.class SOAPTester
All of these return...
Error: Could not find or load main class SOAPTester
Can someone tell me what I am missing here? This is on JDK7 and I can confirm the .class file is in the folder.
From the bin folder, try:
java -cp . org.me.test.SOAPTester
You'll have to execute following when you're in root folder of org folder
java org.me.test.SOAPTester
More details from Why can't I run my java Hello World program if it is inside a package?

Getting java.lang.NoClassDefFoundError when trying the execute a java.class

I created the following class located in the MainJPrint.java file
import com.XXXXX.pdfPrint.PDFPrint;
public class MainJPrint
{
public static void main(String[] args)
{
//System.out.println("Hello World!");
print(".....");
}
public static String print (final String url)
{
Object rc = AccessController.doPrivileged(new java.security.PrivilegedAction()
{
public Object run()
{
...
}
}
}
}
In the same folder I have a jar archive jPrint.jar
I compile the class using the following command
>javac -classpath jPrint.jar MainJPrint.java
When I'm trying to execute resulted class file, I get this error:
>java MainJPrint
java.lang.NoClassDefFoundError: com/XXXXX/pdfPrint/PDFPrint
If I uncomment the Hello World line and comment the next line, the program runs fine.
I'm using j2sdk1.4.2 installed at C:\j2sdk1.4.2.
I do also have installed other java versions (at C:\Program Files\Java: jre 1.6.0_01, jre 1.6.0_02, j2re1.4.2, jre6, jre7, jdk1.7.0_03)
The PATH variable contains the C:\j2sdk1.4.2\bin path, however I think the java.exe is loaded from the upper version, but it shouldn't matter and I can call it like
>C:\j2sdk1.4.2\bin\java.exe MainJPrint
jPrint.jar is a third party archive and I need to create an applet which exposes a method so I can call it with javascript. I'm not a java developer, I'm having some little troubles and I'm really on an end here.
I tried other options like:
>java MainJPrint -cp .
>java MainJPrint -cp jPrint.jar
So how can I execute that class file which uses a class located in a separate archive?
To execute a class that depends on external JARs, you need to specify all elements of the classpath on the command line.
If you don't specify a classpath, Java automatically uses . (the current directory), which is why, if MainJPrint didn't depend on jPrint.jar, your invocation java MainJPrint would have worked.
But when you specify -cp jPrint.jar, Java does NOT automatically add the current directory to the classpath, which means that it then cannot find MainJPrint. You need to specify both. On Mac/*nix, the following invocation should work:
java -cp jPrint.jar:. MainJPrint
Or on Windows:
java -cp jPrint.jar;. MainJPrint

Categories

Resources