Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can javac compile from stdin?

Tags:

java

javac

Can javac compile from stdin? Something like this :

cat myfile | javac
like image 492
kofucii Avatar asked Nov 05 '11 17:11

kofucii


People also ask

What does javac compile to?

The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.

Is javac a Java compiler?

Java is considered as both interpreted and compiled. It uses a Java compiler (javac) and JVM ( which is actually a software-based interpreter) to execute a Java application on a machine completely.

What is source and target in javac?

source: The version that your source code requires to compile. target: The oldest JRE version you want to support.

What does the output of javac contain?

The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)


3 Answers

No, there is no option to do that.

From the documentation:

There are two ways to pass source code file names to javac:

  • For a small number of source files, simply list the file names on the command line.
  • For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character.

Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.

like image 155
Mat Avatar answered Oct 06 '22 04:10

Mat


You might try with /dev/stdin as the source file (and then, you'll need to find the option which forces javac to consider it as Java source code).

But I would just make a shell script which put the stdin on some temporary file suffixed with .java.

But I think (I am not sure) that javac really wants a class Foo to be defined inside a file named Foo.java.

like image 36
Basile Starynkevitch Avatar answered Oct 06 '22 05:10

Basile Starynkevitch


You can't do it with Sun java, but you can write a script to handle the conversion of stdin to something javac can understand.

Something like this Python script:

import fileinput, re, subprocess

class_name = None

buffer = []
class_matcher = re.compile('\w+ class (?P<name>\w+)')

for line in fileinput.input():
    if None == class_name:
        buffer.append(line)
        m = class_matcher.match(line)
        if m:
            class_name = m.group('name')
            print "found %s" % class_name
            file_name = '%s.java' % class_name
            out = open(file_name, 'wb')
            out.writelines(buffer)
    else:
        out.write(line)

if None == class_name:
    print "Error: no class found"

else:
    out.close()

    print 'javac %s' % file_name
    output = subprocess.call(['javac', file_name])

Note that the script will create a file of the name of the class in the current directory. It's probably better to use something in /tmp, but keep in mind that it has to be named the same as the class. If you are testing the script don't do something like this:

cat MyImportantJava.java | python javac-stdin.py
like image 40
Scott A Avatar answered Oct 06 '22 03:10

Scott A