Java [JAVA] I seem to always need to enter the class path to run the program

AI Thread Summary
The discussion centers on troubleshooting a Java program execution issue using the command line. The user successfully compiled their Java program but encountered an error stating "Error: Could not find or load main class hello" when attempting to run it. The program runs correctly when the class path is explicitly specified with "java -cp C:\new hello." To resolve the issue, it was noted that the current directory is not included in the class path by default. A solution involves editing environment variables to add "." to the class path, which represents the current directory. However, this practice is considered unsafe on public or multi-user systems because it can lead to security vulnerabilities, allowing malicious users to replace legitimate files with their own. It is recommended to specify the full path to applications and use the -cp option for class paths to enhance security.
mindauggas
Messages
127
Reaction score
0
Hi,

I wanted to try to use cmd to compile and run a program in java. I have sucessfully compiled it. The compiler is working properly (C:\new> javac output the compilers functions), but I'm always getting the same error: "Error: Could not find or load main class hello".

When I specify the class path ("java -cp C:\new hello") the program works.

How to solve this dilemma to succesfully run the programms in cmd?

P.S. the source file name "hello.java", the class file name is "hello.class" and the specified class is "hello". Even though only the <>.class and the class name itself has to match I wanted to make sure everything is in tact there.

Help really needed here.
 
Last edited:
Technology news on Phys.org
Current directory is not part of the class path by default. You can edit your environment variables and add "." to the classpath (warning: unsafe on 'real' systems).
 
Sorry, but what do you mean by "You can edit your environment variables and add "." to the classpath (warning: unsafe on 'real' systems)"

1. What do you mean by "."
2. And "unsafe for "real" systems?

P.S. I edited the environmental variables. It wokrs fine.
 
mindauggas said:
Sorry, but what do you mean by "You can edit your environment variables and add "." to the classpath (warning: unsafe on 'real' systems)"

1. What do you mean by "."

"." is the name of your current directory, e.g. if you're in "c:\new" then "." == "c:\new".

2. And "unsafe for "real" systems?

Servers out in the world, accessible by the public, or private multiuser systems. In these systems having "." in paths is dangerous because if you're in another directory that is writable by other people, someone could put a program there called "java" and when you say "java hello" it could run their program and not the real java application -- or they could put a "hello.java" in that directory and it would run that instead of yours.

It's always better (read: safer) to always specify the full path to applications (like java / java.exe) and to always specify the classpath with -cp, than it is to change search paths.
 
Thank you
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top