Compiled successfully in Eclipse, but fails using terminal - java

I'm trying to write Java programs in OOP style, and putting two files under the same folder called utility. Also the package is utility.
HelloWorld.java
package utility;
class HelloWorld {
public void hello() {
System.out.println("Hello, world!");
}
}
HelloWorldDriver.java
package utility;
public class HelloWorldDriver {
public static void main(String[] args) {
HelloWorld sayhello = new HelloWorld();
sayhello.hello();
}
}
I used Eclipse to run the HelloWorldDriver.java, which turned out a successful result.
Hello, world!
However, as I was using terminal to run the programs, problems occurred. First, I use javac to compile all the .java files in the folder. Then, I run the file directly.
bash-3.2$ javac *.java
bash-3.2$ java HelloWorldDriver.java
However, the below problems occured.
HelloWorldDriver.java:5: error: cannot find symbol
HelloWorld sayhello = new HelloWorld();
^
symbol: class HelloWorld
location: class HelloWorldDriver
HelloWorldDriver.java:5: error: cannot find symbol
HelloWorld sayhello = new HelloWorld();
^
symbol: class HelloWorld
location: class HelloWorldDriver
2 errors
error: compilation failed
Please help suggest if there's anything wrong about the code or the way I use command lines.

Two things here:
Your error is coming from the way you are trying to run your code in the command line. When you use the java command, you want to specify the compiled class file to run, instead of the java file to run. This would look something like java myProgram.class which is equivalent to java myProgram. Since you are referencing another file in your driver file, you need to add that to the class path so the Java runtime environment knows what to do when you ask it to run functions from that file. In your case, I believe the correct command to run would be java HelloWorldDriver -cp HelloWorld.
Another thing I'd like to mention is that inside your actual code, you don't actually need to create an instance of the HelloWorld class by doing HelloWorld sayhello = new HelloWorld();. Instead you could remove that line and change the line following it to HelloWorld.hello(). There are some instances where you would not want to do this, however for what you are doing, using HelloWorld.hello() will work just fine.

Related

Error: Could not find or load main class in Textpad 8

I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214

Javah tool error: could not find class file for HelloWorld

I am trying to create a header file using javah tool from the command line and an external tool configuration on eclipse on windows 7 OS but it's not working.
My code is:
package mypackage;
public class HelloWorld {
private static String HelloWorld;
private native void print();
static {
System.loadLibrary(HelloWorld);
}
public static void main(String[] args)
{
new HelloWorld().print();
}
}
I have followed different ways and even read the documentation of javah tool from Oracle, but they didn't help to overcome this problem.
My class file (HelloWorld.class) and java file (HelloWorld.java) both are in C:\..\eclipse-workspace\Distribution System Process\src\mypackage
But whenever I run javah tool, it gives me an error:
could not find class file for HelloWorld or mypackage.HelloWorld
I tried by providing classpath as well, but am not getting any header file.
Note: I have two classes in my package. The Frame1.java is the main class, which is the Gui, and the other class is used for JNI and called HelloWorld.java. I am not sure if the classes matter, but I am currently working on HelloWorld.java to create a header file:
What is causing this failure? :(
javah tool requres access to compiled code. You have to provide place, where your compiled class is.
Take a look here for a very simple sample code:
http://jnicookbook.owsiak.org/recipe-No-001/
As you can see, sources are compiled and stored inside some other place (here it is called target)
${JAVA_HOME}/bin/javac -d target java/recipeNo001/HelloWorld.java
${JAVA_HOME}/bin/javah -jni -d c -cp target recipeNo001.HelloWorld
Then, you have to tell javah where to find these files (compiled Java classes). It is done by -cp argument.
Argument -d, on the other hand, will tell javah where to store C headers.

Executing error in java

The principal class XXXX has not been found
The code constructs perfectly but when I try to execute it I get the same error again.
I type the following "Hello world" code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hola mundo");
}
}
I get the same error.
It must be a problem of my program ,I use GEANY because my teacher told me that I can't use netbean or similar.
How do you compile and execute your code? Is it some IDE (Eclipse) or just command line?
IDE should take care about this issue. In command line you have to include all classes into the classpath. Sometime it is confusing because when you compile your code mutually is in classpath but you have to mention it litreally to run.
Here is example
javac Hello.java
compiles Hello.java source to the Hello.class code to the current location. Literally ./Hello.class
To run you should mention this location
java -classpath . Hello
without -classpath . java doesn't know where your class is.

Java can't find or load main

I have recently installed canopy (i don't know if this has anything to do with java)
i have set to system variables the path to javac and java
i was writing compiling and running java programs normally until now that it keeps telling me it can not find or load main class.class
example of code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
what is happening?
ok i changed hello to Hello
i run at powershell:
javac Hello.java
java Hello.class ( i am in the directory of the file)
Error: Could not find or load main class Hello.class
You need to run your program using
java Hello
and omit the .class extension
That should compile fine which Java compiler are you using? Make sure to always save and compile your program before running it.

Java HelloWorld commandline

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. :)

Categories