• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt in classpath

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package com.epl.test;
class TestPack
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

hi
my javac command is javac -d . TestPack.java
it compiled fine


when i run using java -cp com.epl.test; com.epl.test.TestPack.java
it runs.

But when i run using java -cp com.epl.test com.epl.test.TestPack.java
it doesn't run
what semicolon at end of classpath means
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by V Gala:
when i run using java -cp com.epl.test; com.epl.test.TestPack.java
it runs.


Really, is that exactly the command you are typing in? That looks very strange.

But when i run using java -cp com.epl.test com.epl.test.TestPack.java
it doesn't run
what semicolon at end of classpath means


The "-cp" switch needs a path, which is a set of directory names separated by ";" (on Windows) or ":" (on Unix).

Also, when you run a program you should provide the fully-qualified class name, not the name of a source file with ".java" at the end.

If your class is in a package, it should be in a directory structure that matches the package name. So if your class is in the package com.epl.test, you should have a directory 'com', with a subdirectory 'epl', with a subdirectory 'test', and your class file should be in that last directory.

Suppose you have a directory C:\Project\com\epl\test, then the right command to run the program would be:

java -cp C:\Project com.epl.test.TestPack

For more information, see: The Java Tutorial - Creating and Using Packages
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry sir
I type by mistake .java at end
I am using windows


package com.epl.test;
class TestPack
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

hi
my javac command is javac -d . TestPack.java
it compiled fine


when i run using java -cp com.epl.test; com.epl.test.TestPack
it runs.

But when i run using java -cp com.epl.test com.epl.test.TestPack
it doesn't run
what semicolon at end of classpath means
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting something like "com.epl.test;" or "com.epl.test" after the -cp switch is wrong. Doing this will make Java look for a directory named "com.epl.test" (note, this is NOT the same as a directory structure "com\epl\test").

It probably works with the semicolon because that makes Java look in the current directory also.

See the tutorial that I gave you the link to above. Especially look at the page Managing Source and Class Files to understand what directory structure you must use for your package and how to set the classpath correctly.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic