[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 924
  • Last Modified:

How to hard code the absolute path in my java program?

To make question clear here -
Machine - windows platform (os - win2000)
I don't have administrator privilege. I can't install or alter anything else.

I run my java program by using third party java application tools (everything is ok!!!).
If I want to run a java program in a command line i.e. I intend not to use that third party java application tools to run my java program, then I got the message said "java runtime environment not found" and "no java" command something like that. Obviously, the environmental variable and the path linking are not configured properly.
I would wonder that is it possible to write an absolute path in my java program or any idea how to get around this? Then, my java program can run in a command line :(

Thanks
0
htam2000
Asked:
htam2000
  • 4
  • 3
  • 2
  • +5
3 Solutions
 
CEHJCommented:
You need to set up the PATH variable for your account, adding the java bin directory
0
 
CEHJCommented:
You can do that with the System 'applet' in Control Panel
0
 
petmagdyCommented:
or just instead of run:

java urapplication

c:\urjavapath\java urapplication


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kiranhkCommented:
you can create a temp variable on the Environment variables in the Advanced tab of the System applet as told by CEHJ.
otherwise u create a batch file which will set the path and classpath specific to ur application and everytime u open a cmd prompt run the batch file and start executing ur java programs.
something like setEnv.bat
set path=%path%;c:\yourpathfor java
set classpath=%classpath%; c:\your classpath for ur application
0
 
objectsCommented:
Specify the full path to java.exe as petmagdy suggests.
0
 
htam2000Author Commented:
I am getting more confuse.....
What happens with javac?
typed in  ---  c:\>java -fullversion
output    ---  c:\>java full version "1.4.0_04-b04"

typed in  ---  c:\>javac -?
output    ---  'javac' is not recognized as an internal or external comand, operable program or batch file.

What is wrong with that?
I think the path is ok, since  c:\>java -fullversion works.....
How can I compile my java program?
0
 
objectsCommented:
you haven't added the bin directory of your jdk in your PATH, either add it or specify the full path to javac on the command line
0
 
brettmjohnsonCommented:
> output    ---  'javac' is not recognized as an internal or external comand, operable program or batch file.
> What is wrong with that?
> I think the path is ok, since  c:\>java -fullversion works.....


You may have only a Java Runtime Environment installed.  
The JRE can execute java byte-code, but does not include java
development tools such as the compiler, javadoc, etc.
0
 
OviCommented:
I suggest you to use a configuration file, in the same directory as your application (in the application working directory). That configuration file could be a properties file. This way is more flexible than hard-coding the path to the java executable in your code. That path could differ between computers, and you will need to specifically compile the code for each one of them.

Having a properties file is very simple, and can contain a predefined path to the java executable but you will need to instruct your users to set that variale correctly in case the application is not starting.

0
 
htam2000Author Commented:
Would you give me an example of properties file?
I have no idea how to make this file :(
0
 
OviCommented:
A properties file is a file ended with extension .properties., like config.properties
This file contains pairs of key-values elements, like:

java.home.path=c:\j2se1.5.0
java.interpreter.path=c:\j2se1.5.0\bin\java.exe
java.compiler.path=c:\j2se1.5.0\bin\javac.exe

Supposing all the above are stored in a config.properties file, in the working directory of your application.
Insideyour code, you have to do something like:

    String configPath = System.getProperty("user.dir") + File.separator + "config.properties";
    Properties startupProperties = new Properties();

    try {
      startupProperties.load(new FileInputStream(configPath));
    } catch (IOException e) {
      e.printStackTrace();
    }

After you have loaded them in the 'startupProperties' object, you can use the object in the same manner you use an hashtable.

Ex:

String javaHomePath = startupProperties.getProperty("java.home.path");


0
 
OviCommented:
Of course, this is not a solution for storing the path to the java executable. The program won't run in the first place, because is not aware of java. But this is a general way of loading some predefined settings in an application. For more informations on Properties class, please review the API docs for java.util.Properties.

0
 
PaullkhaCommented:
This is just detail of what  CHEJ
Do a search for Javac.exe

If I do a search on MY COMPUTER I find this
C:\jdk1.3.1_02\bin\javac.exe

so I could
a) type C:\jdk1.3.1_02\bin\javac.exe to run javac.exe
or
b)set path=%path%;C:\jdk1.3.1_02\bin
then I can type javac in any directory C:\mycomputer c:\winnt etc.

as far as
set classpath=%classpath%;
Do a search for *.jar, whatever dir comes up, add to this variable as well.

Then run javac in whichever dir you .java files reside.

This can be done also by clicking START>CONTROL PANEL>SYSTEM>ADVANCED>ENV VARIABLE. This only needs to be done once here.
0
 
htam2000Author Commented:
Thank you very much!
0
 
OviCommented:
Thank you too :).
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now