Java: could not find or load main class test - java

I have two .java files in a directory. One of them is an implementation of the Vector data structure, the other is a test class that contains a main method to test my Vector implementation. I'm trying to compile both files at the same time, so I did javac -cp . *.java, then when I run java test, I get Error: Could not find or load main class test, even though after compiling, I now have two .java files and two .class files. What do I do in order to run test from terminal?
Following is my test.java file:
package mStructures;
//import java.util.Vector;
public class test {
public static void main(String argv[]){
Vector<Integer> testV = new Vector<Integer>();
for(int i = 0; i < 39; i++){
System.out.printf("Capacity before adding %d: %d%n", i, testV.capacity());
testV.add(i);
System.out.printf("Capacity after adding %d: %d%n", i, testV.capacity());
}
}
}

For simple things it's best to have Test.java (no package, note the import)
import mStruct.Vector;
public class Test { ... main ... }
in some directory and
package mStruct;
public class Vector { ... }
in its subdirectory mStruct.
Compile and exec:
javac Test.java mStruct/*.java
java Test

First Thing - Your compilation command should have javac instead of java.
Secondly - include the same cp parameter as you have used while compiling the classes.

Related

Compile source files to a different directory and then accessing them in another directory, how? JAVA

My directory structure:
C:\jaBHa
here I want to create two packages - sources and classes.
so it looks like
C:\jaBHa\classes
C:\jaBHa\sources
the sources package must contain all the source files i.e., the .java files.
And then I want them to compile to the classes package which would contain all the .class files.
here are the codes for the three classes...
// MyDate.java
package classes;
public interface MyDate {
void showDate();
}
// DateImpl.java
package classes;
import java.util.*;
import classes.*;
public class DateImpl implements MyDate {
public void showDate() {
Date d = new Date();
System.out.println(d);
}
}
// Test.java
package classes;
import classes.*;
public class Test {
public static void main(String[] args) {
DateImpl d = new DateImpl();
d.showDate();
}
}
Now, I know that there's some issue with my class DateImpl java file, but my objective is...
that I want to compile these java files using the commands
C:\jaBHa> javac -d C:\jaBHa\sources MyDate.java
C:\jaBHa> javac -d C:\jaBHa\sources DateImpl.java
C:\jaBHa> javac -d C:\jaBHa\sources Test.java
with this the MyDate.java, DateImpl.java and Test.java files will compile to classes package...
and then i run it by typing
C:\jaBHa> java classes.Test
as the class file of Test.java is in C:\jaBHa\sources\classes package...
but here's the problem, it compiles to C:\jaBHa\sources\ here it will create a package classes but I want it to compile to C:\jaBHa\classes not C:\jaBHa\sources\classes...
so I have to keep my all java files in C:\jaBHa\ so that it can compile to classes package.
The -d option specifies the output directory for class files.
Try this.
C:\jaBHa>javac -d . sources\*.java
C:\jaBHa>java classes.Test
Mon Oct 24 19:19:05 JST 2022
C:\jaBHa>

How to access other files in any editor?

For example I have Main.java and test.java. test has public static int bro = 5; so i try to print test.bro from Main but the class test is not found. An IDE like Eclipse takes care of this for me but how do I do this with an editor? Sorry noob question. I'm in cmd in the directory of deez files and i type javac Main.java, den java Main. Thanks.
file Main.java:
public class Main {
public static void main(String[] args) {
System.out.println(test.bro);
}
}
file test.java:
public class test {
public static int bro = 5;
}
So suppose you have two source files: Main.java and test.java then you need to compile them first.
You can do it via command javac Main.java test.java. That command will produce 2 files in your current directory: Main.class and test.class. Which contain compiled java code.
Now you need to run your main class with classpath which contains both of your classes. So you need to run command java -cp . Main. Where . represents directory with your compiled classes.

How to compile java class (inside a package) without IDE

I'm working in windows 7, Java 7 and have following folder:
C:\..\myApplicationV
Inside this folder there are two folders with one java class each:
C:\..\myApplicationV\graphics\Circle.java
C:\..\myApplicationV\mains\UseCircle.java
Circle.java contains following code:
package graphics;
public class Circle {
public void describeCircle (){
System.out.println("A circle is round");
}
}
I've been able to compile Circle.java so therefore I have also following file:
C:\..\myApplicationV\graphics\Circle.class
UseCircle.java contains following code:
package mains;
import graphics.Circle;
class UseCircle{
public static void main (String[] args){
Circle circle = new Circle();
circle.describeCircle();
}
}
I try to compile this last one class, I place in:
C:\..\myApplicationV\mains\
and type:
javac UseCircle.java
but I'm getting following message:
UseCircle.java:3: error: package graphics does not exist
import graphics.Circle;
^
Doing some research I have found some information at:
http://docs.oracle.com/javase/tutorial/java/package/index.html
So I have solve this isuue by placing all java classes in one package and works fine. Also I have move UseCircle.java class to base folder:
C:\..\myApplicationV
And also works. The problem is when trying to use the two package. Do you know what could be wrong ?
Please specify the full package path while compiling
cd C:\..\myApplicationV\
javac mains/UseCircle.java
java mains/UseCircle
Open the command prompt in myApplicationV folder & do the following:
javac graphics\Circle.java
javac mains\UseCircle.java
java mains.UseCircle

Opening class in package from console in Java

So when i try to open a java class that's not in a package from the command prompt it all works fine, but when I try to open a class that's in a package it gives me NoClassDefFoundError.
And when I list the package when I try to open the class (java somepackage/someclass) it says that it can't load or find the main class.
Any help?
What I can infer is, you have two classes:
Test.java:
// no package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
so you can compile and run it using:
javac Test.java
java Test
Another class:
Test.java:
package test; // package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
And thus doing same thing gives you error.
For this you need to be in the parent directory of 'test' folder in your terminal or cmd and use:
java test.Test
No problem with compiler. You can compile as usual using javac Test.java from inside 'test' folder.
NoClassDefFoundError means that your JVM can't find your class at runtime. This could be because it's not visible (set to private or protected, or just no access modifier).
It could also be missing from your build path
package pkg1;
public class Dataguise{
public static void main(String [] args){
System.out.println("My name is Maninder Singh");
}
}
Suppose this is my code. My package name is pkg1.
1. First you need to create pkg1 dirrectory if not existed.
2. Run "javac Dataguise.java" command
3. It will generate the "Dataguise.class" file and move this file to "pkg1" folder
4. Now run "pkg1.Dataguise" command it will work.
I was having the same issue so sharing my experience.
What I assume is, you are creating a package and a class inside it, for example.
package com.vishwa.hello.commandLineArgs;
public class ComandLineArguments {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < args.length; i++) {
System.out.println("Command line arg is "+args[i]);
}
}
}
When I try to compile and run the above code in command prompt (from the package folder) as
javac ComandLineArguments.java.
java ComandLineArguments 123 456.
You get the below error:
Error: Could not find or load main class ComandLineArguments
Caused by: java.lang.NoClassDefFoundError: com/vishwa/hello/commandLineArgs/ComandLineArguments (wrong name: ComandLineArguments)
There are 2 ways to solve this:
Go to the root path of your project and then run.
java com/vishwa/hello/commandLineArgs/ComandLineArguments 123 456
789.
Command line arg is 123.
Command line arg is 456.
Command line arg is 789.
If you want to run the program from the package directory, you need to specify the class path along with the complete package
reference.
java -cp
/Users/16399/Documents/workspace-spring-tool-suite-4-4.12.1.RELEASE/CoreJava/src/
com/vishwa/ hello/commandLineArgs/ComandLineArguments 123 456 789.
Command line arg is 123. Command line arg is 456. Command line arg is 789.

Java Command Line Trouble with Reading a Class from a Jar Archive

I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. Does anybody have any ideas concerning this problem? Thank you in advance
Here's a concrete example of what does work, so you can compare your own situation.
Take this code and put it anywhere, in a file called MainClass.java. (I've assumed a directory called src later. Normally you'd arrange the source to match the package, of course.)
package archiveFolder;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("I'm MainClass");
}
}
Then run each of these commands:
# Compile the source
javac -d . src/MainClass.java
# Build the jar file
jar cf archive.jar archiveFolder
# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows
# Execute the class
java -cp archive.jar achiveFolder.MainClass
The result:
I'm MainClass
How are you building your jar file? Is the code in the appropriate package?
Does theMainClassName class have the following package line at the top:
package archiveFolder
You need the class file to be in the same directory structure as the declared package. So if you had something like:
org/jc/tests/TestClass.class
its source file would have to look like this:
package org.jc.tests;
public class TestClass {
public static void main(String[] args) {
System.out.printf("This is a test class!\n");
}
}
Then you could use the following to create the jar file and run it from the command line (assuming the current directory is at the top level, just above org):
$ jar -cf testJar.jar org/jc/tests/*.class
$ java -cp testJar.jar org.jc.tests.TestClass
Perhaps with java -jar archive.jar?
Of course, it supposes the manifest points to the right class...
You should give the exact message you got, it might shed more light.
EDIT: See Working with Manifest Files: The Basics for information on setting the application entry point (Main class) in your jar manifest file.
Usually this happens when a dependent class (static member) is not found - like this, using log4j:
public class MyClass {
private static Logger log = Logger.getLogger("com.example");
}
The reason is that the initialization of such a static member can be understood as part of the class loading - errors causing the class not to be available (loadable), resulting in the error you described.
Static constructors are another possible reason:
public class MyClass {
static {
// <b>any</b> error caused here will cause the class to
// not be loaded. Demonstrating with stupid typecast.
Object o = new String();
Integer i = (Integer) o;
}
}
I think others have covered some common stuff here. I'd jar tf the jar and make sure the class is listed. I'd also double-check that the class is public and the method is "public static void main(String[] arg)".

Categories

Resources