Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to compile my java code using a bash script. I need to pass in arguments too, which I'm getting from another file in the same directory. My script looks like:
#!/usr/bin/env bash
MYID="$(cat $(pwd)/my.id)"
eval javac src/main/java/foo/bar.java "$MYID"
but it thinks of my argument as another java file for compilation and gives me the error:
Class names, 'abc123', are only accepted if annotation processing is explicitly requested
P.S. abc123 is the only id in the my.id file I need to pass.
Please help me make this work, I have been looking for solutions the whole day. :(
edit: This was a stupid question.
I just realized I was passing in arguments at the wrong place. I figured it out. thankyou!
You are going about this the wrong way. The javac command compiles programs. That is it. It doesn't generate source code.
Based on your comments it sounds like you should be doing something like this:
javac foo/Bar.java
java foo.Bar $(cat my.id) > foo/Baz.java
javac foo/Baz.java
This assumes that foo.Bar is some kind of generator program that knows how to generate java source code.
In reality, I expect that what you are really trying to do is some kind of source-code template expansion. There are simpler and / or more flexible ways to do that that writing a bespoke Java program to do it. (For example, use the "m4" utility.)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to make a java program run numerous times by passing the commands with the different arguments to the Ubuntu terminal, through a .txt. But I have no idea how to do it.
Any suggestion?
I find Karol's response to be great, however another suggestion to show there are many ways to do this on Unix :)
Suppose your class is called JavaClass and arguments stored on .txt
xargs -a .txt java JavaClass
Has same effect than
java JavaClass $(cat .txt)
You could use cat to print the a.txt before calling java:
java $(cat a.txt)
It might be easier to convert a.txt into a bash script:
#!/bin/bash
jargs="-version"
java $jargs
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I’m new to python scripting and I need to install Java using python script . please suggest a method to do this.
Thanks all.
If i am not wrong you are looking for silent installation of Java. Here is a link that explains the same:
Silent installation of Java
Next you would want to run this command using python. For this you can use subprocess in python. Below is a link explaining it:
Using subprocess in Python
You want to display error stream too, so use the subprocess.Popen as:
process = subprocess.Popen(['command plus args as in above link'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
out contains the output of the above command and err contains the error stream.
I would suggest you to install java from command line first, and then use this command in python script.
To install mySql or even tcl, follow similar steps,
1. Find how to install using command line
2. Execute the same command using python
Also, there may already be existing packages to do your job, if so you can use them.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to ask you about sentence: "In Java we run classes, not programs". Is it correct? I know the build process, but it seems to me that this sentence is ambiguous
(As this is so wide-open, let's make the answer a community wiki...)
I've never heard anyone say that (and would argue that it's so vague and slightly misleading you should ignore it).
Without context, it's hard to be too specific about what it's meant to mean, but it probably relates to the fact that many of the ways that Java programs are run involve a specific class that is the starting point.
For instance, if you're running a boring old program from the command line:
java DoSomething
...that's saying to run the main method in the DoSomething class.
Similarly, in an executable jar file, the manifest in the jar says which class to run the main from.
Similarly, a servlet is identified by a specific class implementing the appropriate interface and set up in the Java EE container's configuration.
But again, it's a really odd thing to say, not least because although the entry point may be a single class, of course that class then ends up using others to get its work done.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I was thinking of passing the result from Java run from command line into a command line variable. But after searching much for it, I only came across Runtime class which just runs the command.
Therefore, my question, is there anything I can do to get Java result into command line.
Platform of course is Windows.
There are only 2 ways any program (Java or otherwise) can send results back to the "command line":
1) exit code
2) capture the strings output by the program.
#1 can be done in Java using System.exit (or see a couple more options here).
#2 is done by the shell. In Unix, you typically use backticks or $():
OUTPUT=`java app.class`
In Windows Powershell, do something like
$result = & java app.class 2>&1 | Out-String
Possible duplicate of
how-do-i-get-the-result-of-a-command-in-a-variable-in-windows
FOR /F "delims=" %i IN ('java Main') DO set today=%i
Also allows today variable to be set.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Okay, I know there are probably a dozen ways to solve this, but I am looking for either a skeleton app or some sort of tutorial that will explain the best way to write a framework for creating Java-based command-line tools. If my program requires a lot of switches/options/etc., what's the best way to handle all of them?
How do you decide which stuff should be placed into an options/settings file, and which stuff gets put on the command line? Any sort of sample code would be great, that way I can put my time more towards the central focus of my app rather than the command-line plumbing.
I also suggest looking at JCommander (http://jcommander.org/), written by the author of TestNG. I have used it successfully in many command line applications.
How do you decide which stuff should be placed into an options/settings file, and which stuff gets put on the command line?
There's no such dilemma, many things are useful in both places. With the settings file you let the user define defaults and with the command line you let the user override them. Of course, there are cases when only one of the two makes sense, but I'd take providing both as the starting point.
The previous answer seems to cover what you want to know for writing the app. As for your question about what should go in config files and what should be command line options I would recommend this. If the option is something that is likely to have the same value most of the time put it in a config. If its something that changes frequently make it an option, but remember options SHOULD be optional, try not to create a program that someone has to type in allot of required stuff to make it do it's base function.