• 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

launching class file; java

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any difference between these two command line invocations?


1- >java com.sun.sjcp5_prep.MyClass1
2- >java com/sun/sjcp5_prep/MyClass1




Please suggest!



Thanks,
cmbhatt
[ April 19, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1- >java com.sun.sjcp5_prep.MyClass1
"com.sun.sjcp5_prep.MyClass1" is just the name of your class, nothing special about it! I wonder if it is allowed in this way!?
2- >java com/sun/sjcp5_prep/MyClass1
From your current working directory you are invoking a class present in the physical directory structure "com/sun/sjcp5_prep".
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Faisal,




1- >java com.sun.sjcp5_prep.MyClass1
2- >java com/sun/sjcp5_prep/MyClass1

Aren't both invocations do the same thing?
I am in the current directory where the com directory is placed.



Regards,
cmbhatt
[ April 19, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seem to be doing the same thing.
Though i prefer 1 as it gives the true sense of invoking a class.

However, if you comment the package statement in the above class and keep the MyClass1 file in the same location (directory structure as before), and compile

javac com/sun/scjp5_prep/MyClass1.java
//compiles fine

java com/sun/scjp5_prep/MyClass1
Says NoClassDefFound - reason being - it considers the so called directory structure tied to the file - so looks for com.sun.scjp5_prep.MyClass1 and doesn't find a class with the package structure attached to it.

Hence prefer 1 as it gives the true meaning to it.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swarna,

Thanks for your pretty well consideration on this thread;



Your classpath is set to "c:\mysource" which consists the com directory.

So when you invoke java command like
>java com/sun/sjcp5_prep/MyClass1

"com/sun/sjcp5_prep/MyClass1" is the complete name of your class.
and same in the case of

>java com.sun.sjcp5_prep.MyClass1
"com.sun.sjcp5_prep.MyClass1" is the complete name of your class.


What do you say???



Regards,
cmbhatt
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Both command line statements do same thing.
if the question is only related to Packages. Remove vararg in main method
p s v main(String... a)
{
}
instead, type
p s v main(String[] a)
{
}
it works well.

Thanks
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhu,

I don't find any difference between "String[] args" and "String ... args".
If you have any please share with us.


By the way, my question has nothing to do with var-args and String[].



Thanks,
cmbhatt
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, I agreed with your point.
actually I am getting problem when I compile this code with String...
I have replaced with String[] then it worked well.

can you help me on this
my code is:

package Pack1.Pack2;
public class MyClass1 {
public static void main(String... args) {
System.out.println("Hello World");
}
}

and my java version is:
C:\Java>java -version
java version "1.5.0_06"
Java <TM> 2 Runtime Environment, Standard Edition <build 1.5.0_06-b05>
Java HotSpot<TM> Client VM <build 1.5.0_06-b05, mixed mode>

I am able to compile the file
But not able to run. So I have removed String... replaced with String[], compiled with 1.4 version. then compiled succesfully

when I run
C:\Java> java Pack1/Pack2/MyClass1
it says UnsupportedClassVersionError

Please help
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhu,


I am able to compile the file
But not able to run.



Post what error you are getting while trying to run the class.

By the way, var-args came with Java 5.0.


cmbhatt
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,

In my previous mesg pls see the last line for the error which I am getting while running.
yes, I am aware var-args comes in 1.5 .
I am planning write SCJP1.5
I have posted my code[i.e same code posted by you] and compilation, my
java version and error I am getting

Thanks
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,
even I am able to compile but not able to run.
can you explain clearly what you are expecting in your question?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhu,


I am able to compile the file
But not able to run. So I have removed String... replaced with String[], compiled with 1.4 version. then compiled succesfully



Compile using javac -source 1.5 nameoftheclass.java
Then run


Let me check that in detail...
meanwhile I am working on it...


cmbhatt
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhu,

You have classpath setting problem.
If you are using Windows XP :
Go to Control Panel>>System>>Advanced Tab >> Click Environment Variables button. Set the classpath where exactly your "java" is placed as

as on my system: it is


D:\Sun\AppServer\jdk\bin;



in the bin directory you have both javac and java.
And yeah, it should be Java version 5.0 to run your class with var-arg.

Do that way, I hope problem is solved, caution see extra space you might have placed.


Tell me!


Regards,
cmbhatt
[ April 22, 2007: Message edited by: Chandra Bhatt ]
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, my class path for java is correct only, I am able to run other files
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
~~~Ghost~~~ :roll:

You are able to run other files but only not able to run that specific.
What heck have you written in that?

I tried and tested that:
Compile again and then run:
javac and java conflict may be there?
compiled using javac 5.0 and run using java 1.4. (var-arg issue)


cmbhatt
[ April 22, 2007: Message edited by: Chandra Bhatt ]
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic