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
Related
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).
I created a class HT17
package useFul;
class HT17
{
void show()
{
System.out.println("Hello World!");
}
}
And i tried accessing it from another class from same package
package useFul;
class HT18
{
public static void main(String[] args)
{
HT17 h =new HT17();
h.show();
}
}
But i am getting error: Cannot find symbol HT17
Yes, They are in same directory i.e useFul
A solution would be helpful!
At first create a folder named useFul and copy the classes there, then cmd to compile and run. cmd command:
javac useFul/HT18.java
java useFul/HT18
Most likely you are using javac <file name> this will create the class file in the same folder even though you have a package defined.
So you've to use javac -d . option this will create proper folder structure for the classes. Try the following.
$ javac HT17.java -d .
$ javac HT18.java -d .
$ java useFul.HT18
The -d is used to mention where to create the compiled classes with proper folder structure using package. in the example . is used, means use the current directory.
I have problem running a basic helloworld application in Java form my command-line in widnows 7. I can run it in Java.
Here is my code(in NetBeans):
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I have set C:\Program Files\Java\jdk1.8.0_20\bin; on my PATH variable in the Windows Environment.
When running :
javac HelloWorld.java
the HelloWorld.class is built successfully.
However in the next step when I run:
java HelloWorld
I get he following error:
Error: Could not find or load main class HelloWorld
Under my program source root directory I can see these two file:
. HelloWorld.class
. HelloWorld.java
What am I missing please?
You should specify fully qualified class name. That is, you need to run it like that: java helloworld.HelloWorld.
what you need to do is as you have
package helloworld;
and you are trying to execute it from the commandline
do the following steps
First open the terminal or cmd and browse to the folder helloworld.
Example if your helloworld folder in in f:/helloworld open the terminal and browse upto f:/(don't go inside helloworld)
then compile the class as javac helloworld/HelloWorld.java
and try executing the class as java helloworld.HelloWorld
the class name was not fully qualified, try java helloworld.HelloWorld
the .classfile should not be in the directory the java command is run from.
you forgot the helloworld package. So you have to enter java helloworld.HelloWorld to make it work. Next time please use a different package name than the java file, so there is no confusion. :)
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.
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