-1

Is it possible to create simple code such as JAVA or XML in batch file? I am trying below code but the file is not created.

@ECHO off
(   
echo  public class Main {
echo public static void main(String[] args) {
echo    System.out.println("Hello, World!");
echo    }
echo }
) > text.java

pause
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
Blaster
  • 135
  • 3
  • 16
  • 1
    Sure it is possible, but you have to make sure that you get the syntax right. – RhinoDevel Mar 12 '16 at 19:47
  • 1
    You have to escape batch special characters with `^` so `(` becomes `^(`. Trying to do XML will be very ugly as all `<>` need to be escaped. –  Mar 12 '16 at 19:59
  • 2
    @Noodles It's not `(` left parenthesis but `)` right one causing problems inside a `(parenthesized command block)` – JosefZ Mar 12 '16 at 20:18
  • OP, you should also consider a [batch script heredoc function](http://stackoverflow.com/a/15032476/1683264). Then you won't have to worry about escaping parentheses or XML tags. – rojo Mar 14 '16 at 03:12

2 Answers2

3

You need to escape all occurrences of ) right parenthesis in echo commands inside a parenthesized command block as follows:

@ECHO off
(   
echo  public class Main {
echo public static void main(String[] args^) {
echo    System.out.println("Hello, World!"^);
echo    }
echo }
) > text.java

pause
JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

First, off, as a general rule when debugging batch files always remove the echo off line from the top of your batch. CMD is trying to tell you what the problem is and that first line is suppressing all debug information.

In this case, running without suppressing debug information shows you that your main line is causing a syntax error.

echo public static void main(String[] args) {

{ was unexpected at this time.

The error is that certain characters are being interpreted as a special character in the batch file. The way to escape a character in batch is with the caret (^). Adding them to all of the special characters in that line causes echo to produce the correct output.

echo public static void main^(String^[^] args^) ^{

Not all of these escape characters are strictly necessary, but if you're unfamiliar with batch then it can be hard to tell which. The easiest and safest fix is to add an escape before any special character.

There are other syntax errors in your batch. You can continue running your batch with error messages enabled, and fix the remainder the same way.

Community
  • 1
  • 1
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • 1
    The `{` character is **not** being interpreted as a special character in a batch file. Show an example to proof that I'm wrong, please. – JosefZ Mar 12 '16 at 20:13
  • 1
    @JosefZ You are correct. The curly brace is the symptom. The rparen is the cause. The error message isn't clear, though, and escaping both does no harm. – Ryan Bemrose Mar 12 '16 at 20:16
  • 1
    Then. why don't you advise escaping _all_ characters , e.g. `echo ^p^u^b^l^i^c^ ^c^l^a^s^s^ ^M^a^i^n^ ^{`? – JosefZ Mar 12 '16 at 20:36
  • 1
    Because I'm not intentionally being a pedantic jackass. Honestly, what more are you looking for here? I already admitted that your answer is technically correct (the best kind of correct). I even upvoted your answer. For someone versed in the idiosyncracies of the cmd parser, it is useful to know exactly when it's necessary to escape an rparen or a quote or a bracket, and when it's not necessary. For somebody who just wants to write a simple batch file to emit some text, the much simpler rule is to escape all symbols. – Ryan Bemrose Mar 12 '16 at 21:18
  • 1
    IMHO your answer confuse the OP more than supposedly aids him. I imagine the OP escaping _all special characters_ in the Batch files he write from now on just because you suggested that... **`:/`** If _you_ don't know which characters must be escaped and in which cases, then you should not posted an answer (and the same point apply to any other question about any other topic: people that don't know the topic should not post answers). – Aacini Mar 13 '16 at 01:24