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

Click For Summary

Discussion Overview

The discussion revolves around issues related to running Java programs from the command line, specifically the necessity of specifying the class path to execute a program successfully. Participants explore the implications of modifying environment variables and the safety concerns associated with class path configurations.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes successfully compiling a Java program but encountering an error when trying to run it without specifying the class path.
  • Another participant notes that the current directory is not included in the class path by default and suggests adding it to the environment variables.
  • A participant seeks clarification on the meaning of adding "." to the class path and questions the safety of this practice on "real" systems.
  • Further clarification is provided that "." refers to the current directory, and the safety concerns are explained regarding potential risks on public or multiuser systems.
  • It is suggested that specifying full paths for applications and class paths is a safer practice than modifying search paths.

Areas of Agreement / Disagreement

Participants express varying levels of agreement on the safety of modifying the class path, with some acknowledging the risks while others focus on the functionality of the solution. The discussion remains unresolved regarding the best practices for configuring the class path in different environments.

Contextual Notes

Limitations include the potential risks associated with modifying environment variables and the implications of using the current directory in class paths, particularly in multiuser or public systems.

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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
11
Views
3K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K