• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question on running Java programs from the command shell

 
Greenhorn
Posts: 9
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a small question, in the command shell, what is the difference between typing >java HelloWorldApp and >java -cp . HelloWorldApp to run the HelloWorldApp program, they give the same results (printing out "Hello World") so which of them should I use?
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no difference, other than that specifying "-cp ." might discard the CLASSPATH environment variable, but I'm not sure about that. CLASSPATH should not be set anyway.

The current working directory is on the class path by default if you don't override it with the -cp switch. You only have to use -cp if your project depends on non-modular jars or classes that are not part of your project, or if your root package is in a different directory than the current working directory. The latter case is actually very common:

Say your root source package is in a directory named src/ and you want to compile classes to bin/. Your project depends on a jar in the lib/ directory. From your project directory you can run the following commands:
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:There is no difference, other than that specifying "-cp ." might discard the CLASSPATH environment variable, but I'm not sure about that. CLASSPATH should not be set anyway.



It does.

I think the order is:
The classpath defined in an executable jar file manifest (when using java -jar ...)
overrides
the classpath defined using -cp
which overrides
any CLASSPATH defined in the environment.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . I think the order is:
The classpath defined in an executable jar . . .
the classpath defined using -cp . . .
any CLASSPATH defined in the environment.

What happens if you write set CLASSPATH=C:\xyz\pqr\abc\hij;. (Windows®) or export CLASSPATH=/home/critchie/xyz/pqr/abc/hij:. (Unix/Linux/OSX/etc.)? Does that go below -cp and above environment?
 
Saloon Keeper
Posts: 26286
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What happens if you write set CLASSPATH=C:\xyz\pqr\abc\hij;. (Windows®) or export CLASSPATH=/home/critchie/xyz/pqr/abc/hij:. (Unix/Linux/OSX/etc.)? Does that go below -cp and above environment?



"export CLASSPATH=" in Unix/Linux changes the environment. Or to be precise, "CLASSPATH=" changes the environment, "export" exports the change to the parent environment, and combining the two is simply a shorthand.

The "-cp" command-line option should override the environment classpath completely.
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . "CLASSPATH=" changes the environment . . . .

Thank you. But how long for? Let's try it.

[critchie@localhost]$ export CLASSPATH=/usr/java/jdk-13/bin
[critchie@localhost]echo $CLASSPATH
/usr/java/jdk-13/bin

Different terminal open simultaneously:-

[critchie@localhost ~]$ echo $CLASSPATH

Empty result. Even after closing original terminal.
 
Tim Holloway
Saloon Keeper
Posts: 26286
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unlike Windows, which has a global environment and local environments, each and every shell process in Unix-like OS's has its own environment unless explicitly directed to use its parent environment, and will inherit from the parent environment. So setting the CLASSPATH in one shell will not affect the CLASSPATH in any other shell. The "export" directive tells a shell to export the indicated environment value to its parent shell, otherwise when the current shell terminates, that setting is lost.

So, for example, the .rcinit script that runs when you log in must explicitly export JAVA_HOME and CLASSPATH to the login parent shell or the settings made there will not be seen in new shell instances.

And, just to be clear, when a script runs another script, it's spawning a new shell instance to run that script in, so any environment changes made within the called script will be lost unless the child script explicitly exports them to the caller.
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you That is very useful because you can then run several apps concurrently from different terminals with different CLASSPATHs.
 
Tim Holloway
Saloon Keeper
Posts: 26286
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Thank you That is very useful because you can then run several apps concurrently from different terminals with different CLASSPATHs.



Not only CLASSPATHs. It's also how you can use multiple JAVA_HOMEs to run 3 different Tomcat servers under different JVM versions concurrently with a Maven and an Ant.
reply
    Bookmark Topic Watch Topic
  • New Topic