Run from command line, Wrong Name error - java

I want to run a Java project from the command line which I start using a batch file, but I get the wrong name error.
The directory setup:
srcMVC
bin (folder with .class files)
src (folder with .java files)
Batch file
Batch file:
set path=C:\Program Files\Java\jdk1.7.0_09\bin
javac src\model\*.java -d bin -cp src
javac src\controller\*.java -d bin -cp src
javac src\view\*.java -d bin -cp src
javac src\main\*.java -d bin -cp src
PAUSE
java bin\main.Main
PAUSE
Compiling works, but I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: bin\main/Main (wrong name: main/Main)
Any suggestions?
package main;
// omitted imports
public class Main {
// omitted variables
public static void main(String[] args) {
// omitted implementation
}
}

The following statement resolved my error:
java -cp bin; main.Main

NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time.
For example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.
By default Java CLASSPATH points to current directory denoted by "." and it will look for any class only in current directory.
So, You need to add other paths to CLASSPATH at run time. Read more Setting the classpath
java -cp bin main.Main
where Main.class contains public static void main(String []arg)

you are wrongly exicuting java bin\main.main
main() is your main method but you should supply java interpreter the Class Name which implements main()
So if your class name is Test and file name is Test.java which has main() method
java Test
if your Test.java/Test class in is package my.test e.g - package com.my.test;
than, java com.my.test.Test
hope you got it !!

java bin/main.Main is wrong, you must specify -cp here:
java main.Main -cp bin
Here the first argument is the class name which should be found in the classpaths, rather than the class file location. And -cp just adds the logical path to classpaths. You should make the root of your project searchable in the classpath.
and for those javac commands, you have already specified the correct path, so you don't need -cp src. The difference here is the javac command uses logical path for .java files, while using java command you could only specify the path in -cp attribute.
You could also execute java main.Main without -cp if you enter the directory bin:
cd bin
java main.Main
Since the current path will be automatically be searched by java as a classpath.

Assuming you have a class called Main you have to run it with this command:
java bin\Main
It will call your main method.

Java run time (in your case the java.exe command), takes the class file name that containst the main() method as input. I guess you should be invoking it as "java bin\main" assuming there is a main.class which has a public static void main (String[]) method defined.
Note: General practice is to capitalize the first literal of any class name.

Related

Getting "file not found: Main.java" when trying to work with command line in Intellij IDEA

So I wrote same question yesterday but it was closed due to the lack of specifics.
I'm simply trying to run my Main class by terminal. My code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
My classpath: C:\Users\48790\IdeaProjects\workingWithCommand\src
In terminal : Directory of C:\Users\48790\IdeaProjects\workingWithCommand
I've tried : -cp C:\Users\48790\IdeaProjects\workingWithCommand\src and
set CLASSPATH = C:\Users\48790\IdeaProjects\workingWithCommand;C:\Users\48790\IdeaProjects\workingWithCommand\src
Still: file not found: Main.java when typing : javac Main.java
Thanks for helping :)
The compiler (javac) does not use the classpath for the files given as argument.
try javac src\Main.java
or
change to src (cd src) and then javac Main.java
The file given as argument to javac is a file as used for any other command of that operation system. The classes used by the program are searched using the classpath or -cp.
(to execute a class you use java Main (not java Main.class) because the argument is not a file1, but a class name - this is searched using the classpath or -cp option)
1 - with newer Java versions, a single source file can be executed and compiled on the fly by : java Main.java (now it is a file, similar to a script)

java able to compile but unable to find class error with cmd

Hello so recently I have started to transfer from c++ to java and one exercise is to compile and run a java program using cmd.
So okay, I coded my simple HelloWorld program using netbeans and saved it,
package helloworld;
public class Helloworld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
so now my saved .java file is in C:\Users\eatmybuns\Documents\NetBeansProjects\Helloworld\src\helloworld
now I open the cmd and I change the directory to the above and typed
javac Helloworld.java
and now I can see Helloworld.class in the same folder, I read from somewhere that I have to include the package name as well for it to run so I typed
java helloworld.Helloworld
it gave me an error so I tried running it from the src folder instead but it also gave me the same error.
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld
I have read some possible solutions such as using -cp or using -d but it keeps giving me the same error. I am currently using jdk1.8.0_161. on windows 10.
You have to use
java helloworld.Helloworld
and from the parentfolder of helloworld, which is the src directory, in your case.
There is a tight relationship between package and directory structure.
There are many flags you can set for the compiler, like srcdir, targetdir to keep classes and sources apart. But basically, when you invoke your class helloworld.Helloworld, the JVM looks for a directory helloworld/ and expects a Helloworld.class there.
To achive this without compiler flags, you have to put the source into the helloworld/ folder too.
The whole name of your class is helloworld.Helloworld and java should look there and find it there.
mkdir helloworld
mv Helloworld.java helloworld/
javac helloworld/Helloworld.java
java helloworld.Helloworld
Hello world
It's a bit surprising in the beginning, if you don't know it and started with classes without package declaration. But the logic is simple and straight forward: Every package is reflected by the directory structure.
With a distinction of sourcedir and targetdir, the directory structure below has to be the same as without, just the starting point differs. Common target dirs are classes or bin, like in:
javac -s . helloworld/Helloworld.java -d classes
or
javac -s ./src helloworld/Helloworld.java -d bin
But bin or classes don't get part of the package name, and you can't extend the invocation of the class by prepending that dir to the invocation path:
java bin.helloworld.Helloworld
won't work. But
java -cp ./bin helloworld.Helloworld
should. If you read the documentation carefully, you will find, that it carefully distinguishes source file (Helloworld.java), class (Helloworld) and file (Helloworld.class).

How to access another class in another package?

I have 2 classes, each is on different folders
Main class src/mainclass/Main.java
Display class src/swingclass/Display.java
Main Class:
package src.mainclass;
import src.swingclass.Display;
public class Main{
public static void main(String[] args){
Display sM = new Display();
}
}
Display Class:
package src.swingclass;
public class Display{
public Display(){
System.out.println("Display class");
}
}
i run it on cmd and it is stated
error: package src.swingclass does not exist
and also, Main class could not see Display class.
overall, i have 3 errors.
I know that using IDE will make this easier, but i want to learn this on cmd. I followed the tutorials on net, but it still has errors.
I believe you should get rid of the src part of the package, i.e.
package swingclass;
...
and import it like:
import swingclass.Display;
Remove all occurrences of "src." in all files
Change to the src directory
Type: javac mainclass/Main. java
Type: javac swingclass/Display.java
Type: java mainclass.Main
Go to the src folder level (go inside src folder) in your command prompt, and use the commands
javac -classpath . src/mainclass/Main.java
to compile (this will implicitly compile the Display class as well, since it is required by Main class) and
java -classpath . src/mainclass/Main
to run the Main class.
. indicates the current directory (in your case, src folder) relative to which the other paths (to source/class files) are then specified.
javac -d bin -sourcepath src <path to main class from src directory, in your case it will be one folder up from src/mainclass/Main.java>
java -cp bin src.mainclass.Main (set class path to bin root where class files are, scr is where your package starts)
Using the -classpath flag should help you solve your problem.
javac -d "theClassPathNameYouWant" "src/swingclass/Display.java"
javac -d "theClassPathNameYouWant" "src/swingclass/Main.java"
java -classpath "theClassPathNameYouWant" swingclass.Main

Run class in Jar file

If you have a jar file called myJar.jar located in /myfolder and you want to use the class called myClass from it, how do you go about doing it from the command line?
I thought it would be to go into the directory and say java -cp myJar.jar.myClass but that isn't working. Any help would be appreciated.
Use java -cp myjar.jar com.mypackage.myClass.
If the class is not in a package then simply java -cp myjar.jar myClass.
If you are not within the directory where myJar.jar is located, then you can do:
On Unix or Linux platforms:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
On Windows:
java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass
You want:
java -cp myJar.jar myClass
The Documentation gives the following example:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
There are two types of JAR files available in Java:
Runnable/Executable jar file which contains manifest file.
To run a Runnable jar you can use java -jar fileName.jar or java -jar -classpath abc.jar fileName.jar
Simple jar file that does not contain a manifest file so you simply run your main class by giving its path java -cp ./fileName.jar MainClass
Assuming you are in the directory where myJar.jar file is and that myClass has a public static void main() method on it:
You use the following command line:
java -cp ./myJar.jar myClass
Where:
myJar.jar is in the current path, note that . isn't in the current path on most systems. A fully qualified path is preferred here as well.
myClass is a fully qualified package path to the class, the example assumes that myClass is in the default package which is bad practice, if it is in a nested package it would be com.mycompany.mycode.myClass.
This is the right way to execute a .jar, and whatever one class in that .jar should have main() and the following are the parameters to it :
java -DLB="uk" -DType="CLIENT_IND" -jar com.fbi.rrm.rrm-batchy-1.5.jar

I have managed to compile java-program but I cannot execute it

I have just installed JDK on Windows Vista. After that I set proper values for the 4 environment variables: classpath, include, lib, path. After that I was able to compile my HelloWorld-program (I got a *.class file). But when I try to execute the compiled program (I type java HelloWorldApp) it does not work. The Java write a lot of stuff and in the end it is written that it "could not find the main class: HelloWorldApp". Can anybody, pleas, help me with this problem?
Just for clarity; you are saying that you have a class in the default package, that is you have not included a package specifier in the Java file, and your class is called HelloWorldApp. When you compiled this, you got a classfile HelloWorldApp.class in the current directory.
Assuming the above to be true then try:
java -cp . HelloWorldApp
For example, the following works on a unix box:
$ echo 'class HelloWorldApp { public static void main(String []argv) { System.out.println("Hello World!"); } }' > HelloWorldApp.java
$ javac HelloWorldApp.java
$ java -cp . HelloWorldApp
Hello World!
Of course, you should indent your code a little nicer than just shoving the whole thing onto one line ;-)
Edit: To answer the comment:
Normally, the default classpath is the runtime libraries and the current directory. However, if you have the CLASSPATH variable set, then this will override the default, and you need to explicitly set the classpath back to its "default value". To verify if the CLASSPATH environment variable is set, you can do (again assuming unix):
set | grep CLASSPATH
If it is set, that is why you need to manually include . on your classpath.
create a file called HelloWorld.java;
paste the code posted below inside HelloWorld.java:
compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
execute the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld works!");
}
}
How the classpath works, can be read here: http://en.wikipedia.org/wiki/Classpath_%28Java%29
Have you included . and .. in your path? Just for clarification . represents your current directory and .. represents your parent directory. You are telling that the java has to search the current directory and the parent directory to find the class. Add the same to your classpath too.
What happens if you use:
java -cp {path to directory with HelloWorldApp in it} HelloWorldApp
That path should be contained within your CLASSPATH environment variable. Is that exported to your command shell ? Do you need to start a new command shell to get the most recent version of CLASSPATH ?
Post your code. I believe the problem is that your main class is not defined properly. I did this the other day.
public static void main(String[] args){
//code
}
The class path concept and the logical difference between Java source code and compiled byte code is notoriously hard to get right.
I would strongly recommend you familiarize yourself with the Sun Java Tutorial. The relevant section is
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Categories