I have an application that receives a path as a command line argument. The path can contain spaces, so it can be sended with quotes. I need to verify if this path is correct, so I execute 'exists' method from 'File' class:
public static void main (String... args) {
System.out.println("arg=" + args[0]);
File f = new File(args[0]);
System.out.println("exists=" + f.exists());
}
When I run the application with the follow arguments, I obtain this results (assume that "c:\folder" exists). Pay attention with final slash and quotes:
> java Test c:\folder
args=c:\folder
exists=true
> java Test c:\folder\
args=c:\folder\
exists=true
> java Test "c:\folder"
args=c:\folder
exists=true
> java Test "c:\folder\"
args=c:\folder
exists=false
I don't understand what's happens with last example. First in args result don't print final slash and then File class say that path doesn't exists. Second example without quotes works well.
Argument path has a free user edition, so it's possible that can include quotes (if path has folder with spaces) and a final slash.
It is not java issue but your shell. \ act as escape character if it is used before " in Windows. To work around that you can write parameter as "c:\folder\\"
It is also strange output. When i did the same I got args=c:\folder" in last case.
Related
I'm trying to install tomcat as a service using service.bat in the following path :
C:\Program Files\text with' quote\Tomcat
but I keep getting the following error :
java.io.FileNotFoundException: C:\Program Files\text with quote\Tomcat\conf\logging.properties; (The system cannot find the path specified)
as you can see from the error message the ' is being ignored and thus keeping some files from being found/loaded properly.
If I switch to a path without a quote, everything works well. Is there a way around this as I need to include a ' in the path?
Your problem comes from the way Procrun parses its command line parameters. In those parameters which accept lists of values (++DependsOn, ++Environment, ++JvmOptions, ++JvmOptions9, ++StartParams and ++StopParams) single quotes ' are stripped after the parameter value has been split into single values. There is no way to quote them (cf. source code).
Therefore the ++JvmOptions parameter used in service.bat is interpreted as follows (one value per line):
-Dcatalina.home=C:\Scarlett oHara;-Dcatalina.base=C:\Scarlett oHara
-Dignore.endorsed.dirs=C:\Scarlett oHara\endorsed;-Djava.io.tmpdir=C:\Scarlett oHara\temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=C:\Scarlett oHara\conf\logging.properties;
You might notice that some entries are joined by ;, due to the ' unintentional quoting.
The only way to fix this is to start Prunmgr (the executable renamed as tomcat*w.exe) and fix them in the "Java" tab:
-Dcatalina.home=C:\Scarlett o'Hara
-Dcatalina.base=C:\Scarlett o'Hara
-Dignore.endorsed.dirs=C:\Scarlett o'Hara\endorsed
-Djava.io.tmpdir=C:\Scarlett o'Hara\temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=C:\Scarlett o'Hara\conf\logging.properties;
or work directly on the HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0 registry keys.
See also:
a similar problem due to quoting: Adding multiple values to an environment in registry and retrieving in Java application
I'm trying to solve a problem at work, where it's required to access project folders on a shared drive.
However the naming convention is a bit tricky. The URLs on the server start with the static FS\XXX\00 followed by the project number (6 digits long) which is split into pieces of two and between slashes. For example the project folder for project 123456 would look like FS\XXX\00\12\34\56.
What I'm trying to sort out is how to create a .bat file, put it in the environment path and call it with the Run command, so for example I would call the file ex.bat by entering the following sequence in the Run console:
ex 123456
Then the program should split the number, build up and open the following URL:
FS\XXX\00\12\34\56
Any ideas?
%1 is the first parameter. Save it to a variable (%p%) do be able to do substring substitution (see set /?) and build and output the desired string:
#echo off
set p=%1
echo FS\XXX\00\%p:~0,2%\%p:~2,2%\%p:~4,2%
pause
Because powershell was tagged, here's a powershell implementation. Save this as a script in the environment path somewhere:
[CmdLetBinding()]
param
(
[string] $ProjectNumber = '123457'
)
$root = 'FS\XXX\00\'
# Create a string with backslashes every 2 chars
$out = (&{for ($i = 0;$i -lt $ProjectNumber.length;$i += 2)
{
$ProjectNumber.substring($i,2)
}}) -join '\'
# Create final path
$path = Join-Path -Path $root -ChildPath "$out\"
# Run explorer with the path
explorer $path
and call from run/cmd like this:
powershell "& "Script.ps1 -ProjectNumber 123456""
I have ruby code which will create output file, currently file is creating at location from where my script is running.
I have to write output file to different location so i am specifying explicit path into code. but it's not able to create file. my code looks like :
fname = "C:\repo\cookbooks\abc\recipes\add.rb"
somefile = File.open(fname,"w")
somefile.puts "end"
somefile.close
If i specify
fname = "add.rb"
it's working but i want to create it at different location as i mentioned above code in C:\ drive.
Because \ in strings are special characters so you should use \\ (double backslashes) to get a single backslash. But there is a better way, you don't need to deal with backslashes at all:
fname = File.join("C:", "repo", "cookbooks", "abc", "recipes", "add.rb")
I am trying get the parent folder of a Windows user's profile path. But I couldn't find any "parameter" to get this using SHGetSpecialFolderPath, so far I am using CSIDL_PROFILE.
Expected Path:
Win7 - "C:\Users"
Windows XP - "C:\Documents and Settings"
For most purposes other than displaying the path to a user, it should work to append "\\.." (or "..\\" if it ends with a backslash) to the path in question.
With the shell libary version 6.0 you have the CSIDL_PROFILES (not to be confused with CSIDL_PROFILE) which gives you what you want. This value was removed (see here), you have to use your own workaround.
On any prior version you'll have to implement your own workaround, such as looking for the possible path separator(s), i.e. \ and / on Windows, and terminate the string at the last one. A simple version of this could use strrchr (or wcsrchr) to locate the backslash and then, assuming the string is writable, terminate the string at that location.
Example:
char* path;
// Retrieve the path at this point, e.g. "C:\\Users\\username"
char* lastSlash = strrchr(path, '\\');
if(!lastSlash)
lastSlash = strrchr(path, '/');
if(lastSlash)
*lastSlash = 0;
Or of course GetProfilesDirectory (that eluded me) which you pointed out in a comment to this answer.
I am trying to run the following
javac -Xlint:unchecked -classpath C:/Users/a b/workspace/ #C:/Users/a b/workspace/files_to_compile
but I'm getting a
javac: invalid flag C:/users/a
I've also tried to surround both paths with double quotes but it doesn't seem to help a bit:
javac -Xlint:unchecked -classpath "C:/Users/a b/workspace/" #"C:/Users/a b/workspace/files_to_compile"
What am I doing wrong? This same code worked correctly in other computers (probably because they didn't have any white space in their paths..).
Thanks
I've finally come up with the solution to the issue, and I guess no one here could have guessed it.
The cue to the answer lies with the fact that the contents of the files list (signaled as # in the args) generally will have each one of its strings with the initial substring equal to what one passes as both the class path and the # file.
so..
The trouble was never the command line parameters, as suggested, but with the contents of the # file.
Each line of the file must be put in its own line, surrounded by quotes, and having into consideration that if you're in windows, you have to put the file names in the form of C:\\a\\b\\c.txt!!!
Your second try is right
javac -Xlint:unchecked -classpath "C:/Users/a b/workspace/" #"C:/Users/a b/workspace/files_to_compile"
But to be complete, you have to escape the spaces into the text file "files_to_compile" by using:
the same syntax as properties file : \
or
double quote each line
I suggest the second but I'm not sure.
I have to admit this was more difficult than I had imagined.
After some trial and error I came up with the following:
C:\lol>"C:\Program Files\Java\jdk1.7.0_07\bin\javac" -cp "c:\lol\a b;c:\lol\foo bar" Lol.java
where the folder structure is like:
./foo bar
./foo bar/Moo.java
./Lol.java
./a b
./a b/AB.java
I made an archive with the folders and the java files, which you can grab at:
http://www.pvv.ntnu.no/~rakhmato/tmp/lol.tar
You should ignore the # option because it is enough to give the compiler one file and a proper class path, it can figure out where everything is on its own. Just give the compiler your Main.java and it will figure out what that file depends on.
I would also recommend you to write a .bat script of sorts to make things simpler. Nothing fancy, something like this:
compile.bat:
"C:\Program Files\Java\jdk1.7.0_07\bin\javac" -classpath "c:\lol\a b;c:\lol\foo bar" Main.java
..put that in your project folder and run compile.bat from CMD
First using the cd command in shell shift your directory to the one where your file is saved.
cd /home/sayantani/PERSONAL\ FILES/sem\ 4\ courses/PLC/code/
Note that I've used "\" whenever there is space involved. "PERSONAL FILES" becomes "PERSONAL\ FILES".
Then use "javac filename.java"
javac hello1.java
This should fix your problem.
Note that doing "javac" on the entire path from the default directory isn't working.(for me)
You need to escape spaces.
Put a \ in front of each space and try that.
Its taking only the 1st part of the Source String remove the space between a b from the path and it should work fine C:/Users/a_b/workspace/" #"C:/Users/a_b/workspace/files_to_compile" . Never you should have spaces in the path else the latter part will be ignored by the compiler or else you can put a '\' between a\ b
Bit of a hack, but if you're on Windows 7 you can get around this using the mklink utility to create another folder pointing to the same place, but without spaces.
Edit: perhaps a better solution:
cd "C:/Users/a b/"
javac ... -classpath "Workspace" ...
From usage info for "java /?"
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>cd "C:\Program Files (x86)\Java\jre6\bin"
C:\Program Files (x86)\Java\jre6\bin>java.exe
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-classpath indicates that you need to use a semi-colon (";") for multiple paths.
I can't test it but I'd suggest the following (as dmcgil suggested semicolon should be classpath separator on windows):
javac -Xlint:unchecked -classpath C:\Users\a^ b\workspace\;C:\Users\a^ b\workspace\files_to_compile
It seems that the escape charachter for win shell is caret.
That is also suggested here.
EDIT:
Also, in your question, I noticed usage of slashes (/) in paths, doesn't all versions of windows use backslashes(\) as file separators? I saw your comment somewhere on this thread stating just that, so I'll suppose you typoed in question.