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

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

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)

Error: Could not find or load main class ( in Java 8)

I am trying to learn Java and I've made my first program and compiled it into a class file (the file is called aye.java and when compiled I have aye.class, I think the compilation worked). However when I use the java command in the folder where the class is located it just returns below error -
Could not find or load main class aye.class.
I have tried including the package name (com.java24hours) but it still doesn't work.. please help!
Commands I have tried:
java aye.class
java com.java24hours.aye.class
java aye
java com.java24hours.aye
program code:
package com.java24hours;
class aye {
public static void main(String[] args) {
//java code yeet
String aye = "Hello World!";
System.out.println(aye);
}
}
(I am running Linux on a Chromebook and have installed Java via the ppa:webupd8team/java)
Thanks.
I suppose you wanna put binaries to ./bin folder.
Compile aye.java:
javac -d ./bin aye.java
Then cd to ./bin directory and run the program:
cd bin
java com.java24hours.aye
well im stupid
since im new to java, i didn't know anything about packages and such. turns out all i had to do was put the class file in a folder named "ya" (that's the name of the package - i updated the program) and run the command
java -cp /home/ramsey/Documents/ya aye
(-cp stands for classpath, and you use it when you want to specify where you class is located MAKE SURE TO PUT IT IN A FOLDER NAMED AFTER YOUR PACKAGE!!!)
the wiki page is helpful: https://en.wikipedia.org/wiki/Classpath_(Java)
its under the section setting the path to execute java programs
thanks for the help everyone!

Error: Could not find or load main class xxx Linux

I am very new to linux environment.
I am trying to run an simple hello world java class in linux environment.
Hello .java
package com.util;
public class Hello {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("hi");
}
}
I have compiled java class in windows environment and uploaded the .class file to linux system into /home/scripts path.
my command is as follows,
java -cp /home/scripts com.util.Hello
when i am executing this command from this same /home/scripts where Hello.class is there i am getting,
Error: Could not find or load main class com.util.Hello and not able to proceed further.
can some one help me in this issue?
navigate to /home/scripts using terminal
javac com/util/Hello.java
then
cd /home/scripts
java -cp . com.util.Hello
Or,
java -cp "/home/scripts" com.util.Hello
At first you must generate your .class file :
javac ./hello.java
This command has generated hello.class file
And after you can run your class file ! :)
java hello
We first know javac command work well.
I also met this error,and i have resolved this.Let me share this.
First we need to find the parent path of your package in your java codes.
Then cd to that path using java package + fileName should work well at that moment.
I had the exact same issue on windows, and I solved it by adding path "." to both CLASSPATH and PATH, maybe you can try this on Linux as well.
Your .class file should not reside in /home/scripts/, but in /home/scripts/com/util/. Take a look at this document that explains the relation between classpath, packages and directories.
Before Specifying the path,ensure you follow these three things meticulously,
1. Close the command prompt window, before specifying the path.
2. When adding path, add bin and semi- colon at the end and
3. If JAVAC command has worked properly, try java -cp class name.
if you want to run program in current working directory where your class reside.
java gives three options.
first option
java -cp Tester
Second option for current working directory
java -cp . Tester
Third option export CLASSPATH variable
export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or
export CLASSPATH=$PWD
or
export CLASSPATH=
after that you must sorce the bashrc or bashprofile.

Run from command line, Wrong Name error

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.

How to use javap

I must be doing something stupid, because this seems like such a easy operation. For some reason, I just cannot get this command to work for me. I've installed JDK, and I go into the
/bin
folder. I type:
./javap -classpath /home/Matt/workspace/VariablesTestProject/src/ -s VariablesTest.Variable
My actual filepath for the class is
/home/Matt/workspace/VariablesTestProject/src/VariablesTest/Variable.java
The error I get back is:
ERROR:Could not find VariablesTest.Variable
A command like this works however:
./javap -s java.lang.String
These are not the only commands I've tried. I've literally tried every variation I can think of, and none of them work. My javac and java commands both work fine.
Any suggestions?
Javap works against the .class bytecode. So point the classpath at the VariablesTestProject/out or whatever.
If you want perform operations using the javap command, first go to the jar location in cmd prompt.
Eg:
cd /usr/lib/jvm/java-6-openjdk-i386/jre/lib (Linux)
cd D:\javasoftware\Java\jdk1.6.0_43\jre\lib (Windows)
The above locations are the rt.jar locations. java.lang.String is the one of the classes in this jar.
Using javap you can get the information from this class.
Eg:
cd /usr/lib/jvm/java-6-openjdk-i386/jre/lib
javap java.lang.String
One recent use of javap which I've found is to check which version of JDk compiled a particular class file on Windows:
javap -v path to .class file | findstr major
This will give you major version number :
50 - JDK6, 51 - JDK7, 52 - JDK8, 53 - JDK9.
I will make it simple words to understand the use of Javap Command in Java .
javap command is a tool or you can say it dissembler which can read class files .
I have sample java program like below.
class Bank {
private int code = 210;
class SBI {
void display() {
System.out.println("Code is "+code);
}
}
public static void main(String args[]){
Bank b = new Bank();
Bank.SBI sbi = b.new SBI();
sbi.display();
}
}
After compilation I got Bank.class file.Next my friends Subbu and Narasimha asked me compiled version of source file.Later on they want to see what vikram is actually used then they will use the following command to see the basic details like what methods used in this Bank.class file.
C:\ >javap Bank.class
Compiled from "Bank.java"
class Bank {
Bank();
public static void main(java.lang.String[]);
static int access$000(Bank);
}
java has pretty cool tools to check out many features few of them like javaws.jconsole,javaws,jmap,jhat and jinfo
javap is the command used to see the java profile/method declaration of a Inbuilt class/User Defined Class/Interfaces/Abstract Class.
-> If you want to see the methods of a pre defined class then in this path C:\Program Files\Java\jre1.8.0_171\lib you have rt.jar if you installed jdk.
-> Extract that rt.jar file in some folder D:\rt and then go to this folder and type
javap java.util.collection or javap java.lang.* or any other predefined package to see the methods.
-> If you want to see the user defined class methods then go to the workspace\javaproject\specificpackage\bin and then type javap classname and you can see all the methods declaration.

Categories