On Feb 28, 6:17 pm, divyatiwari...@gmail.com wrote:
> 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.
Andrew Thompson - 28 Feb 2007 09:15 GMT
> On Feb 28, 6:17 pm, divyatiwari...@gmail.com wrote:
..
> > p.setProperties("path,C:\Program Files\Java\jdk1.6.0\bin");
..
> 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.
divyatiwari123@gmail.com - 28 Feb 2007 11:45 GMT
<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.
Chris Uppal - 28 Feb 2007 12:45 GMT
> 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