• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Compiling and executing a java program

 
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

Am I mistaken in thinking that the only way you used to be able to compile and execute a java program is like this:
javac path/path/MyFile.java
java path/MyFile
Also, I have noticed that the above only works for me from the command line two directories above the file


From where has all this -cp business come from?

I cannot get -cp to work for me on my Mac. Please help. Thanks for any help that you can give.



 
Clemonte Johnstone
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. Javac and Java are on the path when I type the commands alone they are found in the console
 
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you seen the doc for the javac command:  https://docs.oracle.com/javase/8/docs/technotes/guides/javac/index.html
and the java command: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while compiling with j
avac you must provide location of your java files.  When executing with java command, you should use classpath to set your class location, then use fully qualified name of your class containing main method.

 
Clemonte Johnstone
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks running java using -cp . works. However, providing the above javac command on Mac returns javac file not found
 
Clemonte Johnstone
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies, working for me.
 
Clemonte Johnstone
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason M smith wrote:while compiling with j
avac you must provide location of your java files.  When executing with java command, you should use classpath to set your class location, then use fully qualified name of your class containing main method.



Must this be done from the root directory as if I am outside src the above does not work for me?
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:. . . Must this be done from the root directory . . .

No.

You add the directory to your classpath; use the -cp option rather than setting a system CLASSPATH. I can't remember exactly which level of directory you need to add to the CLASSPATH. I also can't remember where the XXX.class files will be saved.
 
Saloon Keeper
Posts: 12734
277
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use javac and java from any directory. The trick is feeding the right arguments to the -sourcepath, -cp, -d, -p and -m switches.

To compile, use the -sourcepath to specify the folder that acts as the root package of the classes you want to compile. Use the -cp switch to specify JARs and root packages containing pre-compiled classes that your source depends on. You usually do this when you're writing an application that depends on an existing library.

Use the -d switch to specify the output directory of your root package.

As a final argument to the javac command, you specify the path of the entry points that you want to compile. These paths DON'T take the -sourcepath switch into account! ALL paths that you specify are relative to the current working directory!



Imagine that you have an application that consists of the class com.example.Application, whose sources are located in the MyApplication/src/com/example/ directory, where MyApplication/ is the root folder of your project. It relies on a library dependency.jar, which is located inside MyApplication/lib/. You want to compile it all so that your compiled classes appear in the MyApplication/bin/ folder. Assuming you opened a command prompt inside the MyApplication/ directory, here's what you would do to compile:

Note how even though you used -sourcepath to specify that the src folder contains your sources, you still had to specify your entry point as src/com/example/Application.java. As I said before, all paths you specify are relative to the current working directory. Java uses the -sourcepath switch to look for other uncompiled source code that is used by your entry point recursively.



To run the application, you do the same thing, but since your sources are now compiled, you no longer need the -sourcepath and -d switches, and you add your compiled classes to the classpath:

On Mac, I believe you have to separate the classpath entries using a colon instead:



On Java 9+, if your application is modular you use the -p and -m switches instead:

On Mac:
 
Clemonte Johnstone
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for attempting to explain. Difficult to digest. Its still not 100% clear.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us what problem you are facing now.

Please don't quote the whole of the preceding post, which simply adds length to the discussion and no value. Such quotes are liable to removal.
 
Clemonte Johnstone
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please tell us what problem you are facing now.

Please don't quote the whole of the preceding post, which simply adds length to the discussion and no value. Such quotes are liable to removal.



I do not understand the explanation. Rephrasing the Key points would help me to understand
 
Norm Radder
Master Rancher
Posts: 4194
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 Rephrasing the Key points


What are the Key points you need further explanations on?
Here is one:  the javac program is used to compile java source files into .class files.  If the java source files refer to non-Java SE classes, the javac program needs a way to find the definitions for those classes.  The -classpath or -cp command line options give paths to the missing class's definitions that are needed during the compile
reply
    Bookmark Topic Watch Topic
  • New Topic