Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Explain this example

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still confused.Please explain this exapmle?
test|
GetJar.java|
myapp |
Foo.java
public class GetJar{
public static void main(String...a)
{
System.out.println(myapp.Foo.d);
}
}
package myapp;
public class Foo{
public static void main(String args[])
{
public static int d=8;
}
}

A)javac -classpath MyJar.jar GetJar.java
java GetJar

This answer is correct.why java does not use the classpath option.Jvac uses and why java does not use it?

Why this answer is wrong.
javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar

Please clarify my doubt.i AM TOTALLY CONFUSED
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac by default searches for source files in the current directory. java searches for class file in current directory by default. now you are in test directory. right. now when you are compiling GetJar.java , it needs Foo class which is in Myjar.jar. now you have to use classpath switch to specify the classpath where the class files are that are required by GetJar.java. so you specified javac -classpath GetJar.jar GetJar.java .

since you are in test directory(which is the parent of the package root myapp) when you did java GetJar it will search for class files in current directory which is test. now inside test you have GetJar.jar which contains the compiled Foo class.

Remember that in java the classpath should be the parent of the package root. here the package root is myapp and its parent is test . so test should be in classpath which it is, because you are in test directory.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java -classpath MyJar.jar GetJar means go to the MyJar.jar file, but cannot find GetJar class file.
For javac, -classpath includes the classes that are needed to compile a java file.

For java, -classpath should include the classes that are needed to compile a java file, as well as the path to find the class file that contains the main method.

java -classpath GetJar means go to the current directory, find MyJar.jar as well as GetJar.class , and execute GetJar class .
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vvus bharadwaj wrote:I am still confused.Please explain this exapmle?
test|
GetJar.java|
myapp |
Foo.java
public class GetJar{
public static void main(String...a)
{
System.out.println(myapp.Foo.d);
}
}
package myapp;
public class Foo{
public static void main(String args[])
{
public static int d=8;
}
}

A)javac -classpath MyJar.jar GetJar.java
java GetJar

This answer is correct.why java does not use the classpath option.Jvac uses and why java does not use it?

Why this answer is wrong.
javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar

Please clarify my doubt.i AM TOTALLY CONFUSED



And please use code tags. otherwise nobody would bother answering your question.
 
vvus bharadwaj
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i tried another example.Look at this.I do not specify any classpaths i.e in environment variable

This is the following directory structure.

Source|
pack1|
File1.java
pack2|
File2.java
I compiled the file2.java successfully and i got the class file.
Now i import the pack2.File2 in File1.java and i used the imported file in file2 class.I compiled the file2.java in pack1 directory only using "javac File2.java".It also successfully compiled.Why it has been compiled.You said that Javac look for only source files in current directory by default.I used the file2 class and it is in pack2 directory.I think javac looks for the source files and class files it needed in current directory by default.Is this right?

I dont know how to run this program.I used the following commands

I am in source directory.I used this command
java -classpath . pack1.File2
You said that we have to give the parent of root package directory.But this is not working.Please give methe correct command
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the previous post:
"java -classpath . pack1.File2" won't work because File2 is not in pack1 package.
 
vvus bharadwaj
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry.I posted it wrong.Actual command is

java-classpath . pack1.file1
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will this actual command work?
What is the error message? ClassNotFound?
Does File1 have a main method?



I compiled the file2.java in pack1 directory only using "javac File2.java"



If File2 is in pack2, you may want to compile it under pack2.
Suppose your current directory is in source directory, try to use javac -classpath . -d pack2 pack2\Fil2.java to see if it works.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic