File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes launching class file; java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "launching class file; java" Watch "launching class file; java" New topic
Author

launching class file; java

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

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

Joined: Feb 28, 2007
Posts: 1707
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 ]
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
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

Joined: Feb 28, 2007
Posts: 1707
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
madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 21, 2007
Posts: 100
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 21, 2007
Posts: 100
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

Joined: Apr 21, 2007
Posts: 100
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 21, 2007
Posts: 100
No, my class path for java is correct only, I am able to run other files
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
~~~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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: launching class file; java