Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create New Firefox Profile in Java with Spaces?

I can easily create a new Firefox Profile in a custom directory using the command:

    String profileName = "ABC XYZ";
    Runtime rt = Runtime.getRuntime();

    String dir = "C:\\My Custom Dir\\profileName";


    String executeCommand = "cmd.exe /c start firefox -CreateProfile \""  +  profileName.replaceAll("\s", "_")  + " " + customFirefoxProfileDir + "\"";

    Process process = rt.exec(executeCommand);  

The problem I'm encountering is that if I don't replace empty spaces in the profileName with an underscore (or some other character) the Profile will be generated only using the first word in profileName (i.e, ABC instead of ABC XYZ).

After researching this for some time, I believe this queer phenomenon stems from the fact that Firefox does not allow spaces in the Profile Name.

-CreateProfile profile_name

Create a new profile in the default directory, but do not start the application. The profile will be named profile_name in the profile manager, the profile_name must not contain spaces().

While I can always automatically replace empty spaces with underscores, the problem is that there's no way for me to enforce this convention as some of the profiles are being created manually through the Choose User Profile Dialog with spaces.

Oddly, when clicking Create Profile in the Firefox - Choose User Profile dialog I am able to name it whatever I want - including with spaces.

So my question is: Is there a way to create a new Firefox Profile programmatically (in Java) while including spaces in the actual Profile Name?

I tried surrounding the profileName with double quotes but it didn't work.

like image 779
alpha1 Avatar asked Aug 26 '26 01:08

alpha1


1 Answers

B.t.w. here is the 21 year (!) old bug entry that explains why this is not possible: https://bugzilla.mozilla.org/show_bug.cgi?id=51509

but maybe you can learn more from the accepted answer to this question?

Programmatically create Firefox profiles

In any case I would recommend using the alternative method for runtime.exec that takes an array to avoid messing with extra quotes:

rt.exec(new String[] {"mkdir", "C:\\my test"});

And in doubt watch with sysinternals procmon what is really happening behind the screens.

Also consider dynamically composing a .bat or .cmd or similar script file that you can execute from Java and is easier to write and debug. Example for a createprofile.cmd file:

@echo off
REM Put REM before the first line if you want to debug this script

IF "%~1"=="" ( echo please pass two arguments: the profile name and directory && goto END )
IF "%~2"=="" ( echo please pass two arguments, the profile name and directory && goto END )

REM Use %~1 instead of %1 to get rid of the double quotes of the invocation
set "FF_PROFILE_NAME=%~1"
set "FF_PROFILE_DIR=%~2"

IF exist "%FF_PROFILE_DIR%" ( echo directory already exists ) ELSE ( mkdir "%FF_PROFILE_DIR%" || exit %ERRORLEVEL%)

REM See: http://kb.mozillazine.org/Profiles.ini_file
set "PROFILE_CONFIG=%APPDATA%\Mozilla\Firefox\profiles.ini"

REM Let's make a backup first
copy "%PROFILE_CONFIG%" "%PROFILE_CONFIG%.bak-%RANDOM%%RANDOM%" >NUL

REM Find the highest profile number
REM TODO: Performance, stop looking if we found the highest profilenumber
REM TODO: Stop with error if we reach the highest profile number that we check for
FOR /L %%G IN (0,1,99) DO (findstr "\[Profile%%G\]" "%APPDATA%\Mozilla\Firefox\profiles.ini" >NUL && set "LAST_PROFILE=%%G")

SET /a NEXT_PROFILE=%LAST_PROFILE%+1

REM Write the new configuration section the ini file, appending it to the end:
ECHO.>> %PROFILE_CONFIG%
ECHO [Profile%NEXT_PROFILE%]>> %PROFILE_CONFIG%
ECHO Name=%FF_PROFILE_NAME%>> %PROFILE_CONFIG%
REM Escape with caret ^ the meaning of file descriptor numbers
ECHO IsRelative=^0>> %PROFILE_CONFIG%
ECHO Path=%FF_PROFILE_DIR%>> %PROFILE_CONFIG%
ECHO.>> %PROFILE_CONFIG%

:END
like image 156
JohannesB Avatar answered Aug 27 '26 15:08

JohannesB