How to access packages? - java

I wrote a program HelloWorld.java
and stored in a folder(package) named test that test includes hello folder by itself.
and all in my workspace.
I mean this way: d:\workspace\test\hello\HellWorld.java
And I entered the d:\workspace in my path environment, my code:
package test.hello;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("HelloAll");
}
}
When I go to the hello directory at CMD and compile HelloWorld.java everything is fine and done.
but as I use java HelloWorld (in d:\workspace\test\hello) I get exception in thread main error.
Can you help me with this just simple question?

You must use the fully qualified name of your class to run it.
Stand in d:\workspace\
Run:
java test.hello.HelloWorld

in cmd windows go d:\workspace and issue the following cmd
d:>workspace>java test.hello.HelloWorld

cd to d:\workspace
Compile using-
javac -d . HelloWorld.java
The above will create package structure.
Run using-
java test.hello.HelloWorld

You need to use java command from your source directory i.e. d:\workspace as mentioned here:
java test.hello.HelloWorld
The syntax is simple, just go to your source code directory and not the package directory. Use the classname along with full package name.

Related

Cannot run Java app on cmd using Java11 and Windows

I ahve tried several approaches as on How do I run a Java program from the command line on Windows? and create exactly the same class and packages to use the same things.
Here is the sample class located on C:\SimpleJavaProject\src\com\hello\programs:
package com.hello.programs;
public class ABC {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compile it in the usual way:
C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java
Later, run it by giving the package name and then my java class name:
C:\SimpleJavaProject\src > java com.hello.programs.ABC
In each time I try to run java app, I get "Error: Could not find or load main class com.hello.programs.ABC" error. Then have a look at What does “Could not find or load main class” mean? page and tried some approaches on that page. But still the same error.
It is too simple, but still I have not managed to run the simple app yet. So, how to fix this problem? And after running the app, how can I pass args on cmd?
Update: I could already generate ABC.class file by running the following command. BUT, I cannot run the app and see the "Hello world" on the console.
cd C:\SimpleJavaProject\src\com\hello\programs
javac ABC.java
--> generates ABC.class in C:\SimpleJavaProject\src\com\hello\programs
java com.hello.programs.ABC
When you run this, java is going to check each and every CLASSPATH path for that + /com/hello/programs/ABC.class, will load that, and then run it.
This must mean that either:
[A] Your classpath does not include the current directory; The fix is java -cp . com.hello.programs.ABC.
[B] you didn't do what you wrote, and e.g. dir com/hello/programs.ABC.class prints nothing.
Note that you're not doing it right; a class file should never be in a directory path that includes src. If you don't want to bother with build tools like maven or gradle, I strongly suggest you don't bother with a src dir then either. If you must, the -d option can be passed to javac to tell it where tou put the file. If you want to separate source and class files, then that should be targeting a directory named bin or build or whatnot (a sibling of the src dir).
When you use javac ABC.java you are compiling the class, and javac places it in the current directory.
So java com.hello.programs.ABC would not work (because com/hello/programs/ABC.class file does not exists).
You can use the javac -d flag:
-d <directory> Specify where to place generated class files
For instance:
> javac -d . ABC.java
> java com.hello.programs.ABC
Hello world
> cd com\hello\programs
> dir
ABC.class
Would work, because javac did place ABC in com/hello/programs.
Update: For clarity, once you compiled using javac -d . ABC.java you can run it using java com.hello.programs.ABC and you should see Hello world in the screen.

Could Not Load Main Class (Java)

Probably this is a repeated question, but I just couldn't find an answer to what I am looking for! I am trying to compile and run a java class in a Unix box.
I have the class as:
package tmp.test;
import org.jasypt.registry.AlgorithmRegistry;
class Algo {
public static void main(String[] args) {
System.out.println(AlgorithmRegistry.getAllPBEAlgorithms());
}
}
The files are in the path /tmp/test/. Now I compile the class with the command:
javac -cp jasypt-1.9.3.jar Algo.java
The JAR file is in the same directory. It compiles just fine. But when I run the class file with the command:
java -cp jasypt-1.9.3.jar Algo
I get the error:
Error: Could not find or load main class Algo
I am executing all the commands from the path /tmp/test/.
I tried:
java -cp jasypt-1.9.3.jar tmp.test.Algo
and
java -cp jasypt-1.9.3.jar tmp/test/Algo
Both throw the same error.
I am not sure what I am doing wrong. At first I thought it was the problem of the access thing. So I changed everything using chmod to 777. Everything seems to be fine. Can you please let me know what I am missing here?
I am executing all the commands from the path /tmp/test/
That is the problem. You need to be one level above tmp, not somewhere inside. Then your command line
java -cp /path/to/jasypt-1.9.3.jar tmp.test.Algo
should work. If you insist in starting Java from the subdirectory inside your classpath, you can do this quite contrived thing:
java -cp /path/to/jasypt-1.9.3.jar:../.. tmp.test.Algo
tl;dr
Use the switch -d to compile and then use the fully qualified name of the class to run it.
Compile the class as follows:
javac -d . -cp jasypt-1.9.3.jar Algo.java
The switch, -d specifies where to place generated class files and . stands for the current directory.
Run the class as follows:
java -cp jasypt-1.9.3.jar tmp.test.Algo

Java compiles my program, but I cannot run it

I want to run a Java file with the following source code:
package u0a1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run the file I did the following things:
C:\.. \u0\u0a1> javac HelloWorld.java (this works, class file is created)
Then I try to run it with:
C:\..\u0> java u0a1.HelloWorld
This step doesn't work. Main class could not be found.
I also tried
C:\..\u0\u0a1> java HelloWorld
C:\..\u0> java u0a1\HelloWorld
None of them worked.
This is a piece i found somewhere else, worked for me.
Have you set your JAVA_HOME correctly? If not you have to work with
the full path
Example: "C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe"
HelloWorld.java
If you have runtime issues, you should work it out like this
Select MAIN directory - not package directory
java u0a1/HelloWorld
If you have problems with CLASSPATH or JAVA_HOME - try this:
"C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe" HelloWorld.java
source: http://quandano.com/questions/how-to-run-a-java-file-within-a-package-from-cmd
You are compiling a program of package , so it should compile this way
C:.. \u0\u0a1> javac -d . HelloWorld.java
here -d for creating package u0a1
and "."
for from current working directory
after compiling this way a folder will create with name "u0a1"
then other thing will work properly

How to run a simple java class on the command prompt in windows 8

I'm having a bit of trouble trying to run a java class on the command prompt. Its a very simple class and a friend of mine says it could be a problem with windows 8. Are there any suggestions. Here i'll show you the class and how I tried to compile it. I works fine in eclipse.
package gmit;
public class A {
public static void main(String[] args) {
System.out.println("hello");
}
}
In the command prompt I wrote
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>
followed by - javac A.java
java A.class
I tested the class using type A.java
and the text from the class does come up.
What am I doing wrong? Any help would be great.
If it runs in an Eclipse project, but not from command line, you could try this:
C:\Users\eclipse\workspace\Oct1stcasting\bin\gmit>java A
Inside a project directory, Eclipse saves source codes in /src and bytecodes in /bin. Thus, if you simply want to run your bytecode from command line, changing directory to /bin might suffice.
To compile
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>javac A.java
To run
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>java A
Don't append the .class extension while running the java program
Don’t add a .class suffix when using the java command.
Use java A or java -cp . A. The latter is required if the current directory is not implicitly used as class path.
Compilation:
C:\Users\eclipse\workspace\Oct1stcasting\src>javac gmit/A.java
Executing:
C:\Users\eclipse\workspace\Oct1stcasting\src>java gmit/A

NoClassDefFoundError error while running JAVA console app

I created little HelloWorld example and I am having problem running it from command prompt (on Windows). When I attempt to run it by:
java tcpServer from command prompt I get NoClassDefFoundError
I am able to compile it with javac, and class file gets generated.
Somewhere I was reading that I have to put path to my class folder into CLASSPATH environment variable. I've done it and rebooted machine, but I still get the same error.
I also was trying to run it by java -cp c:\MyFolderWhereClassFileIs HelloWorld, it doesn't work.
I've looked into ENV variables and I have following:
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.6.0_26;
JRE_HOME:C:\Program Files (x86)\Java\jre6;
CLASSPATH: C:\HelloWorld;
So, how do I run this?
Any ideas how to resolve this? Thanks.
PS. The most annoying thing to me is, if I create java project in eclipse, and create HelloWorld example, then it runs fine...
UPDATE:
Here is the code. It does have package specified.
package test.com;
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
My HelloWorld.java and HelloWorld.class files are here:
C:\workspace\TestApp\src\test\com
One thing I learned so far is that I can't run it from within com folder or test folder. I have to be in src folder class file can be found... but I am still not able to run it... always the same error.
Does your class have a package name? That is, a statement at the top which reads
package <mypkgname>;?
If so, then you have to (A) create the correct directory structure and (B) prefix the class name with the package name in your java command.
For example, if your Java file looks like this:
package hello;
public class HelloWorld {
...
}
Then a basic directory structure would resemble:
src/hello/
src/hello/HelloWorld.java
src/hello/HelloWorld.class
And your bash command would be:
cd src
java hello.HelloWorld
Otherwise, if your class has no package name, it will be placed in the default package. In this case, you simply have to cd to the directory where your class file resides, and run java HelloWorld.

Categories

Resources