I have a slight pet peeve when I try to compile and run my java programs in the terminal
javac example.java
When I want to run java example and use autocomplete on the java command, it automatically includes the period (java example.). Of course, this is standard behavior as it matches the longest prefix, but is there a way to cut off the trailing period here? I wouldn't mind editing certain configuration files and I believe I saw other computers slicing off the period. It would be especially nice if this functionality is exclusive to the terminal command java.
As comments stated, the answer was to use bash-completion. I used the following git-repo as a reference. As for my case on a Mac (taken straight from the link), the instructions are as follows:
brew install bash-completion
and inside ~/.bash_profile, the following lines are added:
if [ -f `brew --prefix`/etc/bash_completion ]; then
. `brew --prefix`/etc/bash_completion
fi
Related
I want to execute some old code in java, but it's becoming complicated with old versions of it. Therefore, I wanted to create my "Java aliases" so the command "java11" would execute the java 11 binary command. Unfortunately, on mac, I was not successful.
I tried to follow tutorials wanting to add an alias part in your ~/.zshrc. I did that, and it works perfectly, but it doesn't work for bash scripts, which is really annoying.
Here is the line for my java11 alias in the .zshrc:
alias java11="/Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home/bin/java $#"
(I don't know if it would do anything, but I also added the alias lines in my .bashrc)
Aliases have limited usefulness for things like this. They need to be configured in a shell initialization file (and which file is used depends on the shell, what mode it's started in, etc), they're disabled by default in scripts, and they aren't available at all when the command is executed by something other than a shell (e.g. in sudo java11 ..., it's sudo that does the execution, and it doesn't know anything about aliases).
I'd recommend creating an actual command instead. Since this is just another name for an existing binary (/Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home/bin/java), you can just use a symbolic link to the original binary (for something more complex, it might make sense to create a wrapper script instead). /usr/local/bin doesn't exist by default in macOS, but it is in the default PATH (in most contexts except cron jobs, grrr), so if you create it, it'll be searched automatically. So something like this should work:
sudo mkdir -p /usr/local/bin
sudo ln -s /Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home/bin/java /usr/local/bin/java11
In your running zsh sessions, you'll probably have to run rehash to get them to notice the new binary. And remove the alias, to keep it from overriding the new command.
it's a good doc for people who first use java
but meet about command line i have a few questions
https://lift.cs.princeton.edu/java/windows/
His terminal code like this:
~/Desktop/hello> ls
Barnsley.java COS 126.iml WELCOME.txt logo.png
~/Desktop/hello> javac-introcs Barnsley.java
~/Desktop/hello> java-introcs Barnsley 10000
but in my idea,it uses cmd.exe so ls should be replaced dir i know
but when i type javac-introcs Barnsley.java
it tells me
'java-introcs' is not an internal or external command, nor is it a runnable program
Or a batch file.
enter image description here
The output you observed means that you haven't completed the installation of some class-specific programs. The installer for this is mentioned in Section 0 of the link you provided. The most likely cause is that there were some environmental variables that were not modified correctly to add the java-introcs executable or alias to %PATH%. However, you are probably able to replicate the intent of java-introcs, as described below.
To figure out how to make the given Barnsley file compile and run, you'll need to add the dependency StdDraw.class to the classpath when running the file. To do this, you can use the java and javac option -classpath or -cp. You can read more detailed documentation on how to do this here.
I run my java source code through a batch file. The problem is, when I make any change in my source code through any text editor like notepad etc. and save the code, the changes do not reflect back when I run the code through my batch files.
Below is the sample of my batch file that I use to run my code.
cd %cd%
set classpath=%cd%\target\classes;%cd%\lib\*
echo %classpath%
echo %cd%
java org.testng.TestNG %cd%\code.xml
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %cd%\test-output\report.html
pause
Disclaimer: This was started as an edit for paulsm4's answer. But in my opinion this change got too big to be a justified edit.
Changes you make to source files will never take effect until you recompile your code.
You can do this by:
Calling javac (or an equivalent java compiler) manually or within your batch file.
Usage of javac based on this documentation:
javac [ options ] [ sourcefiles ] [ classes ] [ #argfiles ]
Arguments may be in any order.
options
Command-line options.
sourcefiles
One or more source files to be compiled (such as MyClass.java).
classes
One or more classes to be processed for annotations (such as MyPackage.MyClass).
#argfiles
One or more files that lists options and source files. The -J options are not allowed in these files.
Have an IDE (like eclipse) do it for you
The IDE will build the application on run if the code has changed
Use a build tool like:
Ant
Gradle
Maven
A build tool combined with an IDE is what most developers do. A build tool is also used for dependency management. Which makes it great if more than one person works on a project, or when the project is moved between machines often.
Of COURSE the "changes you make in notepad" won't take effect ... until you RECOMPILE YOUR code.
You need to execute the "javac" compiler (or equivalent) before the next time you run "java".
Which is precisely why C/C++ programmers use "make", and Java programs have used "Ant".
STRONG SUGGESTIONS:
Get an IDE (I prefer Eclipse, but there are other, excellent choices):
https://www.eclipse.org/downloads/
Learn a build tool (Ant is still very useful):
http://www.mkyong.com/tutorials/apache-ant-tutorial/
A quick'n'dirty workaround is to simply add "javac" to your .bat file. But that means you recompile every single time you run the .bat file - whether you need to or not. That works OK when your program consists of one or two source files ... but it doesn't scale when your app grows to 10s or hundreds of source files.
Familiarize yourself with an IDE, and familiarize yourself with build tools. Time well spent :)
Check, if the source code is newer than the compiled code (I have no idea about Java, so you surely have to adapt the extensions - feel free to adapt it in this answer too to help others, who might stumble over this)
for %%F in (program.txt) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetimeSource=%%I
for %%F in (program.exe) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetimeCompiled=%%I
if "%datetimeSource%" gtr "%datetimeCompiled%" echo insert here your command to compile the code...
(See here for the reasons for that strange syntax)
wmic is quite slow, so this code snippet needs about a second, but it's the most reliable way to get a comparable format.
I have around 600 java classes in a project with random names (there is no pattern in their names).
I want to replace a particular line from inside these classes with another line.
For e.g. line inside these files is
System.out.println("Product is: "+myProduct);
and I want to replace that line with
System.out.println("Object is: "+myObject);
The above line is just an example showing what I want to achieve.
I am using RAD 7.5 for development.
Is there a simple way to do this in all 600 files at once?
Yes. By opening the Search - File... dialog box, entering what you want to search for and the file filter you want, and then clicking Replace.
No. No access to UNIX system.
That's your first problem. Install Cygwin so you have a solid shell, and then you can learn how to do batch operations on source code. Being able to grep around easily can make it much easier to learn your way around large code-bases and narrow down the amount of code that is likely to contribute to a thorny problem.
Once you've got that installed, you can solve your immediate problem with
perl -i.bak \
-pe 's/System.out.println\("Product is: "+myProduct\);/System.out.println("Object is: "+myObject);/' \
file0.java file1.java ...
Breaking it down:
-i means treat the arguments as files to modify in-place
.bak means make a copy of the input files with the .bak extension before making changes
-p means wrap the program in a loop that grabs a line, runs the program, and prints the line
-e 's/.../.../' means the quoted part is the program to run, and it does a regular expression substitution.
the rest are the files to run it on.
Alternatively,
find . -name \*.java
should list all the java source files under the current directory.
Assuming that directory doesn't have spaces in its path, you can then do
find . -name \*.java | xargs perl -i.bak -pe 's/.../.../'
where the ... is the replacement above to run it over all Java sources under the working directory.
I'm not sure what to tag this with, but I need help combining two commands into a single command. I've tried this with ant, but it doesn't perform as required (long story). Essentially I need
javac *.java
java org.junit.runner.JUnitCore filename
To be consolodated into a single command. Preferrably something like
ant
with an external build.xml file, however after several hours fiddling with Ant I've gotten it to work but not as required (I need continuous output to stdout during runtime). I'm fine with shell scripts, clever java tricks, anything. I'll take what I can get at this point.
Writing a shell script is really a trivial matter: just copy-paste those exact two lines into a file (say you call it myscript.sh) and you're done. Then you can run it with sh myscript.sh. For added convenience add a first line that says #!/bin/sh (the so-called "hash-bang" incantation), issue a chmod u+x myscript.sh, and then you can run it as any other command: ./myscript.sh.
BTW this turned out into a question unrelated to Java, you might retag it with a shell script-related tag.