• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

launching application with java command line

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been stuck doing this for few hours, i appreciate if someone can help me out!

I have two classes;



and:


I'm running ubuntu 11.10 and i have tested java and javac and they work.

The following is my directory structure:


the current directory is src

I used:
and it created the class file TestingClient.class inside src/test
and it created the class file Client.class inside Testing/classes

now when i use:

i get this error message:
Error: Could not find or load main class test.TestingClient

what i'm doing wrong?
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i put both classes inside bin folder and ran the java command line it worked! so could it be the -cp?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your classpath option.

1) As a rule never forget "." (Current directory) in your classpath.
2) To run a java class within a package, provide fully qualified class name.

Play with -classpath option for a while. It will work.

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have used the -d switch of javac while compiling both the classes.
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
unbelievable! it worked when i typed:



which i don't get it? my Client.class is inside classes.fadi how did the (.) find the Client.class?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems you've changed the package name from test to fadi. Ignoring that, you compiled one of your class in src and one in classes folder. So when you entered the command

java -cp ../classes test.TestingClient

the JVM didn't look for any class in the src folder. If TestingClient was also compiled in classes folder it would've worked. When you tried

java -cp ../classes:. test.TestingClient

This time the JVM looked for classes in both classes folder and current (src) directory so it was able to find both Client and TestingClient classes...
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:It seems you've changed the package name from test to fadi. Ignoring that, you compiled one of your class in src and one in classes folder. So when you entered the command

java -cp ../classes test.TestingClient

the JVM didn't look for any class in the src folder. If TestingClient was also compiled in classes folder it would've worked. When you tried

java -cp ../classes:. test.TestingClient

This time the JVM looked for classes in both classes folder and current (src) directory so it was able to find both Client and TestingClient classes...



but my current directory is:

so i still need the (.) for test.TestingClient class even though i'm running the java command from current directory src?

sorry about changing the package name, i was trying so many things last night to figure out the problem!
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not have all the class files in the classes directory?

javac -d ../classes Client.java

Use same like above while compiling all the classes.

java -cp ../classes Client

Use like above while running all the classes.

-d switch tells where the class files should be placed. So you always know all the compiled class files reside in the classes directory and use the same one for setting the classpath while running using java command
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Why not have all the class files in the classes directory?

javac -d ../classes Client.java

Use same like above while compiling all the classes.

java -cp ../classes Client

Use like above while running all the classes.

-d switch tells where the class files should be placed. So you always know all the compiled class files reside in the classes directory and use the same one for setting the classpath while running using java command



John, i'm just trying to be prepared in case i get such question which i will most likely get.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
haha - I just forget that this is certification forum
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default the javac looks for classes in the current directory. But if you override the classpath using environment variable or -cp/-classpath then you need to include the current directory as then the current directory is not automatically searched for class files...
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit clear as a whistle, thank you all for your time and help.
 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont see how you managed to compile the files using javac TestingClient.java if your current directory was src.
 
reply
    Bookmark Topic Watch Topic
  • New Topic