• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Compile & Run Java program

 
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my mother.java and mother.class in directory
C:\Name\Henry

How do I compile and run when my cursor is at this directory
C:\Name

Thanks!
 
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways to do it. See whether you can get any of the following to work:-Remember that in Java11+ you might be able to omit the javac instruction, but you should look up the full details of single file execution first:-
 
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm not hijacking this thread, but I'm having no luck compiling a simple program using the -cp option.

I wrote a trivial class:
It's compilable and runnable:
Now I cd up a directory and try to compile with -cp:
What's going on?
 
Saloon Keeper
Posts: 11184
244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The -cp option is for classes, not source files. It stands to reason that if you want to run a single source file application, you simply specify its path relative to the current working directory, rather than using a separate command line switch to first specify the source directory:
 
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't seem to be explicitly stated in the docs but javac expects the file path for the source files you are compiling. As Knute noticed, putting the source file's directory on the classpath doesn't work. Campbell's second and third examples won't work because of that. The java command expects <package>.<mainclass> as the argument, though, so we have to use -cp to run it from another directory.
Instead of
you would have to do

and if you want to use set CLASSPATH you would still have to pass javac the path of your .java file
 
Jj Roberts
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may be worth mentioning that javac does search the classpath for other classes which your class depends on:

javac docs wrote:The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file, but that provide information through inheritance.
...
When the compiler needs type information, it searches for a source file or class file that defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath option


Say I have two classes
main.java:Greeter.java:
From the '.' directory I can compile them like this (it might be better to use -sourcepath instead of -cp for clarity):
To run I have to have all of the classes on the classpath:
 
henry leu
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jj Roberts wrote:It doesn't seem to be explicitly stated in the docs but javac expects the file path for the source files you are compiling. As Knute noticed, putting the source file's directory on the classpath doesn't work. Campbell's second and third examples won't work because of that. The java command expects <package>.<mainclass> as the argument, though, so we have to use -cp to run it from another directory.
Instead of
you would have to do

and if you want to use set CLASSPATH you would still have to pass javac the path of your .java file



JJ Roberts   THANKS!!!
 
henry leu
Ranch Hand
Posts: 36
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jj Roberts wrote:It may be worth mentioning that javac does search the classpath for other classes which your class depends on:

javac docs wrote:The compiler needs type information for every class or interface used, extended, or implemented in the source file. This includes classes and interfaces not explicitly mentioned in the source file, but that provide information through inheritance.
...
When the compiler needs type information, it searches for a source file or class file that defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the CLASSPATH environment variable or by using the -classpath option


Say I have two classes
main.java:Greeter.java:
From the '.' directory I can compile them like this (it might be better to use -sourcepath instead of -cp for clarity):
To run I have to have all of the classes on the classpath:



Hi JJ Roberts,

What does the colon ":" between java -cp dir2:dir1 do?
 
Jj Roberts
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the java command documentation, the section on the -classpath or -cp option:

--class-path classpath, -classpath classpath, or -cp classpath
A semicolon (;) separated list of directories, JAR archives, and ZIP archives to search for class files.

Specifying classpath overrides any setting of the CLASSPATH environment variable. If the class path option isn’t used and classpath isn’t set, then the user class path consists of the current directory (.).

As a special convenience, a class path element that contains a base name of an asterisk (*) is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. A Java program can’t tell the difference between the two invocations. For example, if the directory mydir contains a.jar and b.JAR, then the class path element mydir/* is expanded to A.jar:b.JAR, except that the order of JAR files is unspecified. All .jar files in the specified directory, even hidden ones, are included in the list. A class path entry consisting of an asterisk (*) expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, is similarly expanded. Any class path wildcard expansion occurs before the Java VM is started. Java programs never see wildcards that aren’t expanded except by querying the environment, such as by calling System.getenv("CLASSPATH").


Notice the line I underlined. The -cp option specifies any number of directories to put on the classpath. You specify all of the directories you need, separating them with either a colon (on Linux and I think Mac too) or a semicolon (Windows). From your file paths, I assume you are on Windows. Say you needed classes from C:\Java\MyProject\ and also needed classes from C:\Java\Assignments\Lesson4\, and you are compiling a class in C:\Java\ (forgive me if my example file paths are wrong, I am not a Windows user). To put them both on the classpath you would have as part of your javac and java command.

By the way, I highly recommend reading through the documentation for both the java and javac commands.
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have been away and come back to find I have got it wrong. Sorry. Well done JJ for all the work you have put in to correcting my mistake.
 
Jj Roberts
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry Campbell, we all get things wrong. It is rare for you!   I love the opportunity to help other people with something I am confident about; I've still got a long way to go learning Java.

Thank you for the cow!
 
Stephan van Hulst
Saloon Keeper
Posts: 11184
244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, great job. I think I have another cow to spare.
 
Jj Roberts
Ranch Foreman
Posts: 76
9
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, wow! Thank you!
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jj Roberts wrote:. . . Thank you for the cow!

That's a pleasure
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!