Could not find or load main class package.class - java

I know this question has already been answered many times, but unfortunately I couldn't find the right answer to my questions.
below is my package structure and inside my package I have SimpleTest.java
d:\junit\src\junitfaq\SimpleTest.java
inside d:\junit\src> i tried to compile SimpleTest.java and it successfully compiled using the command below.
d:\junit\src>javac junitfaq/SimpleTest.java
but when i try to run the program using command line below
d:\junit\src>java junitfaq.SimpleTest
this error occured. Error: Could not find or load main class junitfaq.SimpleTest
I tried running it by accessing junitfaq package by using this command
d:\junit\src\junitfaq>java -cp . SimpleTest
the program run perfectly. A little help would be much appreciated.

Have you declared your SimpleTest class to be a member of the junitfaq package? If you have, you should be able to run it from the src directory like java junitfaq.SimpleTest but you should get an error like this if you try to run it from within the junitfaq directory: Exception in thread "main" java.lang.NoClassDefFoundError: SimpleTest (wrong name: junitfaq/SimpleTest)
Make sure your SimpleTest class starts with package junitfaq;
Edit: Here's a working example incorporating the comments below.
login#domain:~/temp> mkdir src
login#domain:~/temp> cd src
login#domain:~/temp/src> mkdir junitfaq
login#domain:~/temp/src> nano junitfaq/SimpleTest.java
The contents of SimpleTest.java are as follows when I exit nano:
package junitfaq;
public class SimpleTest {
public static void main(String[] args) {
System.out.println("Test");
}
}
login#domain:~/temp/src> javac junitfaq/SimpleTest.java
login#domain:~/temp/src> java junitfaq.SimpleTest
Test

It sounds like you have a classpath problem; you should double-check the location of your class file, the directory/package structure, the location from which you're trying to run the java command, and any classpath specified during execution.
For example, the following works for me:
$ mkdir junitfaq
$ cat >junitfaq/SimpleTest.java
package junitfaq;
public class SimpleTest
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
^D
$ javac junitfaq/SimpleTest.java
$ java junitfaq.SimpleTest
Hello, world!
$ java -cp . junitfaq.SimpleTest
Hello, world!
$
Not to belabor the obvious, but I noticed a spelling typo in one of your comments - you should also double-check that you're running the command as intended.

trialYou could try putting your java and class files together with your libaray folder (if any) in a new file called "trial" in your C:\ directory then compile existing java file using the following->
C:\trial> javac -cp .;library folder* SimpleTest.java
(then)
C:\trial> java -cp .;library folder* SimpleTest
Let me know how you get on!

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 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

How can I avoid Error: Could not find or load main class

I'm starting to learn java and this is my program:
package Hello;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
I opened the terminal and went to my working directory:
cd /path/to/directory/
Then compiled the program:
javac Hello.java
Then attempted to run it:
java Hello
And I get this:
Error: Could not find or load main class Hello
But if I remove package Hello; everything works.
Why does removing that line make the program work and what can I do to make my program work and still keep the package line?
I looked around, and can't find a clear answer to my question. People say
type java -cp . Hello and don't use .class after Hello
but that doesn't work for me.
If your package is "Hello", java assumes it is located in folder named "Hello".
So if you did:
cd /path/to/directory/
mkdir Hello
mv Hello.java Hello/
javac Hello/Hello.java
java Hello.Hello
It should work. But I agree with Pitchers, package name should be lowercase.

How do I create a Java Package and Test it Using Vim in Linux

I know how to create a java package, write code in the /src folder and test it from the /tst folder. But, I want to learn how I can do that without Eclipse, just using Vim.
For example, I have a java Hello World Class:
class HelloWorld {
public void disp() {
System.out.println("Hello World");
}
}
I save this in a directory home/javacode/HelloWorld
Now, I write a test for it in the same folder :
import org.junit.*;
class HelloWorldTest extends HelloWorld {
#Test testHelloWorld() {
HelloWorld hello = new HelloWorld();
hello.disp();
}
}
Now, can someone please help me as to how to get this test to run? As in I have to compile Hello World as :
javac HelloWorld.java
and then compile the test as :
javac -cp "path to junit" HelloWorldTest.java
However, I always end up with a ClassPath error. I understand that I am really messing up something very fundamental here. Please do help me out with this!
Try to compile the test with:
javac -cp ".:path to junit" HelloWorldTest.java
Note the .: which means "current folder plus ..."
I'm pretty sure the current folder is not in the classpath by default for security reasons.

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?

Categories

Resources