Search code examples
javabashtextslurm

Weird Characters Added When Creating File From String In Java


When trying to create a bash script (.sh file) in Java, random characters are added to the beginning of the file.

As a result, the script is rejected by SLURM when submitted using the sbatch command.

My Code:

String ss = "#!/bin/bash\n"; // script String
        ss+= "#SBATCH --job-name="+scriptJobName+"\n" // scriptJobName is a String that's initialized earlier

try{
    FileOutputStream fos = new FileOutputStream(scriptName); // Where scriptName is a String that is initialized earlier.
    DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
    outStream.writeUTF(ss);
    outStream.close();
}catch(IOException e){
    System.err.println("ERROR: writing sub-job execution script "+e);
}

Script Created:

#!/bin/bash
#SBATCH --job-name=ColComp

[Editor's Note: The first line of the generated script is preceded by two invisible characters, 0x05 and 0x01, which seem to be visible only in edit mode, even when formatted as code. This seems to be the counterexample to "never post pictures of code" :-) enter image description here ]

SLURM Submission Error:

sbatch: error: This does not look like a batch script. The first sbatch: error: line must start with #! followed by the path to an interpreter. sbatch: error: For instance: #!/bin/sh

Asks

How can I prevent these unwanted characters from being added? (or automatically remove them - in Java)


Solution

  • The problem is you used a DataOutputStream. That is unnecessary here, just write to the FileOutputStream. The 0x05 0x01 is an object header that is meaningful only when the output is read later by a DataInputStream.