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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top