How can I beautify JavaScript code using Command Line?

I am writing a batch script in order to beautify JavaScript code. It needs to work on both Windows and Linux.

How can I beautify JavaScript code using the command line tools?


First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at, because it’s what I found first. Downloads its file

Second, download and install The Mozilla group’s Java based Javascript engine, Rhino. “Install” is a little bit misleading; Download the zip file, extract everything, place js.jar in your Java classpath (or Library/Java/Extensions on OS X). You can then run scripts with an invocation similar to this

java -cp js.jar name-of-script.js

Use the Pretty Print/Beautifier from step 1 to write a small shell script that will read in your javascript file and run it through the Pretty Print/Beautifier from step one. For example

//original code    
(function() { ... js_beautify code ... }());

//new code

Rhino gives javascript a few extra useful functions that don’t necessarily make sense in a browser context, but do in a console context. The function print does what you’d expect, and prints out a string. The function readFile accepts a file path string as an argument and returns the contents of that file.

You’d invoke the above something like

java -cp js.jar beautify.js file-to-pp.js

You can mix and match Java and Javascript in your Rhino run scripts, so if you know a little Java it shouldn’t be too hard to get this running with text-streams as well.


Differences between unix and windows files

Am I correct in assuming that the only difference between “windows files” and “unix files” is the linebreak?

We have a system that has been moved from a windows machine to a unix machine and are having troubles with the format.

I need to automate the translation between unix/windows before the files get delivered to the system in our “transportsystem”. I’ll probably need something to determine the current format and something to transform it into the other format. If it’s just the newline thats the big difference then I’m considering just reading the files with the As far as I know, they are able to handle both with readLine. And then just write each line back with

while (line = readline)
    print(line + NewlineInOtherFormat)



This is only a difference in text files, where UNIX uses a single Line Feed (LF) to signify a new line, Windows uses a Carriage Return/Line Feed (CRLF) and Mac uses just a CR.

to which Cebjyre elaborates:

OS X uses LF, the same as UNIX – MacOS 9 and below did use CR though


There could also be a difference in character encoding for national characters. There is no “unix-encoding” but many linux-variants use UTF-8 as the default encoding. Mac OS (which is also a unix) uses its own encoding (macroman). I am not sure, what windows default encoding is.


In addition to the new-line differences, the byte-order mark can cause problems if files are treated as Unicode on Windows.


However, another set of problems that you may come across can be related to single/multi-byte character encodings. If you see strange unexpected chars (not at end-of-line) then this could be the reason. Especially if you see square boxes, question marks, upside-down question marks, extra characters or unexpected accented characters.


On unix, files that start with a . are hidden. On windows, it’s a filesystem flag that you probably don’t have easy access to. This may result in files that are supposed to be hidden now becoming visible on the client machines.

File permissions vary between the two. You will probably find, when you copy files onto a unix system, that the files now belong to the user that did the copying and have limited rights. You’ll need to use chown/chmod to make sure the correct users have access to them.

There exists tools to help with the problem:


If you are just interested in the content of text files, then yes the line endings are different. Take a look at something like dos2unix, it may be of help here.


As pauldoo suggests, tools like dos2unix can be very useful. Note that these may be on your linux/unix system as fromdos or tofrodos, or perhaps even as the general purpose toolbox recode.

Help for java coding


When writing to files or reading from files (that you are in control of), it is often worth specifying the encoding to use, as most Java methods allow this. However, also ensuring that the system locale matches can save a lot of pain


This is only a difference in text files, where UNIX uses a single Line Feed (LF) to signify a new line, Windows uses a Carriage Return/Line Feed (CRLF) and Mac uses just a CR.

Binary files there should be no difference (i.e. a JPEG on a windows machine will be byte for byte the same as the same JPEG on a unix box.)


How do I open the default mail program with a Subject and Body in a cross-platform way?

How do I open the default mail program with a Subject and Body in a cross-platform way?

Unfortunately, this is for a a client app written in Java, not a website.

I would like this to work in a cross-platform way (which means Windows and Mac, sorry Linux). I am happy to execute a VBScript in Windows, or AppleScript in OS X. But I have no idea what those scripts should contain. I would love to execute the user’s default program vs. just searching for Outlook or whatever.

In OS X, I have tried executing the command:

open mailto:?subject=MySubject&body=TheBody

URL escaping is needed to replace spaces with %20.

Updated On Windows, you have to play all sorts of games to get start to run correctly. Here is the proper Java incantation:

class Win32 extends OS {
    public void email(String subject, String body) throws Exception {
        String cmd = "cmd.exe /c start "" "" + formatMailto(subject, body) + """;


In Java 1.6 you have a stardard way to open the default mailer of the platform: the Desktop.mail(URI) method.The URI can be used to set all the fields of the mail (sender, recipients, body, subject). You can check a full example of desktop integration in Java 1.6 on Using the Desktop API in Java SE 6

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..