• Post Reply Bookmark Topic Watch Topic
  • New Topic

sourcepath environment variable  RSS feed

 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a quick google search, I noticed that tools like ant and jbuilder recognise the sourcepath environment variable but the javac command does not. Why is this? The javac command only recognises the -sourcepath command-line option.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. It certainly sounds like a mistake to support sourcepath as an environment variable, because sources should be specified on a project basis, not by the environment.

Always use the command line option, never the environment variables. The same goes for the -classpath option.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The same goes for the -classpath option.

I think that Java gives us various options for setting the classpath for convenience.

If you want to set the classpath for every java command, you can use the java command’s –classpath option, for example java -classpath c:\helloWorldFolder greet.HelloWorld

If you don't want to keep typing the same -classpath option for every java command you type in a particular command prompt window, you can temporarily set the CLASSPATH environment variable for a particular command prompt window via the DOS set command, for example set classpath=c:\helloWorldFolder. You can then type multiple java commands in this command prompt window as follows java greet.HelloWorld without specifying a -classpath option.

If you use your system for trivial applications and your system always uses the same classpath and you don't want to keep typing the same DOS set command in every command prompt window, you can permanently set the CLASSPATH environment variable in the Control Panel (or the autoexec.bat file in Windows 98).

Nevertheless, I get what you're saying. With the second and third options, it's possible to use the wrong classpath for a java command, but if you know what you're doing, the various options can make your life a little easier. 
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:You can then type multiple java commands in this command prompt window as follows java greet.HelloWorld without specifying a -classpath option.

If you use your system for trivial applications and your system always uses the same classpath and you don't want to keep typing the same DOS set command in every command prompt window, you can permanently set the CLASSPATH environment variable in the Control Panel (or the autoexec.bat file in Windows 98).


I don't see why you would want to do this. Class paths should be different per project. If you want to run a command multiple times for the same project, you can just navigate your command prompt history with the up and down keys.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For convenience. Some might prefer to type rather than use the up and down keys; in which case, you don't want to keep typing the same -classpath option over and over again. I agree that classpaths should be different per project; however, if you're using a particular command prompt window for a particular project, you don't want to keep typing the same -classpath option over and over again. Instead, you can set the classpath once for that particular command prompt window via the DOS set command and then carry on with your work with less typing.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:Instead, you can set the classpath once for that particular command prompt window via the DOS set command and then carry on with your work with less typing.


This is quite reasonable, so long as you remember...;)
It's similar to using BAT files (or scripts) and defining CLASSPATH inside them.  For example catalina.bat.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you start working on projects with multiple source files you'll want to use a build tool instead of compiling and running your code manually.

Popular build tools for Java are Maven and Gradle. You create a build file (in the case of Maven it's called pom.xml) that describes the structure of your project and the dependencies (libraries that the project needs), and Maven will then take care of automatically downloading the dependencies and putting them in the classpath while compiling the project.

If your program is a stand-alone Java program you'll want to package it in an executable JAR, so that the only thing you'd have to type in to run it is: java -jar myprogram.jar

The classpath for an executable JAR is specified in the manifest file, which is inside the JAR file.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!