help with properties class

D

divyatiwari123

What am I doing wrong in this program??
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



import java.util.Properties;



class PropEg
{

public static void main(String arg[])

{

Properties p;
p.setProperties("path,C:\Program Files\Java\jdk1.6.0\bin");

String pvalue = p.getProperty("path");

System.out.println("Property path="+pname);


}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


I'm getting the following errors:

PropEg.java:13: illegal escape character
p.setProperties("path,C:\Program Files\Java\jdk1.6.0\bin");
^
PropEg.java:13: illegal escape character
p.setProperties("path,C:\Program Files\Java\jdk1.6.0\bin");
^
PropEg.java:13: illegal escape character
p.setProperties("path,C:\Program Files\Java\jdk1.6.0\bin");
^
3 errors
 
A

Andrew Thompson

What am I doing wrong in this program??

Good question.
//////////////////////...

(Note these big long lines are not needed,
most people expect code to be enclosed in
just <code> .. </code> or <sscce> .. <sscce>
or similar.)

import java.util.Properties;
...

There are a number of problems with
this code sample, the first of which
you identify in the output.
I'm getting the following errors:

PropEg.java:13: illegal escape character
p.setProperties("path,C:\Program Files\Java\jdk1.6.0\bin");
^
PropEg.java:13: illegal escape character

Java regards '\' as an 'escape' character,
to indicate the next character should be
treated in a special way. For example..
'\n' will cause a new line in a text area,
or '\t' will cause a 'tab'.

In order to get a '\', you need to tell
Java that you really *mean* a '\' and the
way to do that is '\\' - put two of them.

That way, Java knows you actually mean
simply '\'.

Note that when it comes to paths, using
hard coded file separator characters
(the '\') causes cross-platform problems.

That might not be important to this code,
but I think it would be worth investigating
why you believe your application needs to
set this property, as well as the entire
way you are doing it.

(Oh, and the second problem is that method
requires two arguments of type string, and
is singular, but more later.. ;)

Andrew T.
 
A

Andrew Thompson

On Feb 28, 6:17 pm, (e-mail address removed) wrote: ... ...
Note that when it comes to paths, using
hard coded file separator characters
(the '\') causes cross-platform problems.

A better way to express that might be..

<sscce>
import java.io.File;

class JavaHome {

public static void main(String[] args) {
String sJavaHome = System.getProperty("java.home");
File fJavaHome = new File(sJavaHome);
// use the x-plat way to get a file separator.
File fJavaBin = new File( fJavaHome, "bin" );

// the 'toString()' is not strictly needed
System.out.println( fJavaBin.toString() +
" \texists: " + fJavaBin.exists() );
}
}
</sscce>

HTH

Andrew T.
 
D

divyatiwari123

<sscce>

import java.util.Properties;

class PropEg
{
public static void main(String arg[])

{

Properties p=new Properties();
p.setProperty("path","C:\\Program Files\\Java\\jdk1.6.0\\bin");

String pvalue = p.getProperty("path");

System.out.println("Property path="+pvalue);

}
}

</sscce>

Hey thanks Andrew. It worked with the escape sequences, but it doesn't
solve my purpose. I wanted to be able to set the class path while
executing the program. Is that possible?

P.S. Can you please explain your code a bit. Am new to this field.
 
C

Chris Uppal

I wanted to be able to set the class path while
executing the program. Is that possible?

No.

What you /can/ do is use a classloader to load files from wherever you want,
but that's a fairly advanced technique, and probably isn't what you are looking
for. What are you trying to achieve ?

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top