I'm calling vim/gvim from within a Java program to convert a source code containing file into html, as follows: "gvim -c \"set syntax=java\" -c \"TOhtml\" -c \"wq\" -c \"q\" -c \"q\" " + Konst.FPATH + "tmp.txt"
However, for some reason gvim thinks "syntax=java" is a name of a file, and ends up spitting out tons of messages ".syntax=java.swp" found, and more importantly does not apply syntax highlight after all. How to fix this?
EDIT: For some reason, "set syntax=java" is misinterpreted, and gvim opens a file syntax=java" -- exactly this, with a double-quote at the end.
Executing an external program from Java is generally not a good idea, especially if this code run on a web server. It might lead to serious security issues.
What are you trying to do? If your goal is to highlight some Java code, just use a code highlighting library.
I don't know any Java highlighting library, but you can find some good Javascript libs (highlightjs, prismjs, code-prettify, etc.).
I solved this with "gvim -s script.vim" command, where in script.vim I put (!) ":set syntax=java" etc -- yes, starting with ":".
Related
I have been trying to get ANTLR to work all day. I have used several websites including antlr.org, numerous StackOverflow queries and the textbook by Terence Parr. I am on a Windows 7 machine (work machine, can't change to Linux) but I have tried this on both CommandPrompt and Cygwin, with the same result.
No matter what I do, when it comes time to use the grun file, I always come up with the same error in the end:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Hello as lexer or parser
I have the most recent versions of the JRE and JDK on my computer, as well as the most recent version of cygwin.
I can run the batch files for grun and antlr4 from anywhere, so I'm strongly guessing my path is set correctly. I can run antlr4.bat with absolutely no issue. It creates every file the text book says I should see:
Hello.g4 HelloBaseListener.java HelloLexer.tokens HelloParser.java Hello.tokens HelloLexer.java HelloListener.java
I can run javac on those files and it generates various CLASS files:
HelloBaseListener.class HelloLexer.class HelloListener.class HelloParser$RContext.class HelloParser.class
But then, when I try to run grun on it, I get this:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Hello as lexer or parser
I've tried several combinations of commands to use the grun file, but the one it says to use in the book is:
grun.bat Hello r -tree
I've also changed the .bat file with grun several times, just to experiment, using different combinations in the books and on the sites, but I always come back to this:
java org.antlr.v4.runtime.misc.TestRig %*
That is what my boss told me to use, and it works for him.
Use this in your grun.bat command
java org.antlr.v4.gui.TestRig %*
I had the same problem, there is actually already a thread with the solution here --> antlr4-Can't load Hello as lexer or parser
The first part of the message "Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically" just means that the TestRig order is saved to another location, it works anyway but I changed my batch data accordingly and did not get the Warning anymore. The second part of the message is the actual problem (see link). In my case I forgot the "." (dot) in my CLASSPATH in Windows.
I have a program which throws StackOverflowException. So the error output is very big and I can't read beginning of output from terminal. How can I watch all program output?
I know that there is something like pagination in terminal. I've googled about it and found advice to use more or less commands.
So I've tried...
java Program | less
But it doesn't work. What am I doing wrong?
Assuming you are using bash4 you can use the |& to concat std error.
java Program |& less
You can redirect the output to a file
java myProgram &>file.log
Then you can tail , head or grep that file.
Use a good IDE (Integrated Development Environment) such as Eclipse. Any decent IDE will have a debugger mode.
Java is a both compiled and interpreted language, which gives it speed and is also easy to debug.
IDE debuggers will walk you through the program execution step by step, which can help you to find errors in your program that you might not otherwise see.
My first question in stack overflow... Kind of excited but still struggling in the problem.
Alright, My question is how to pass parameters from command line to a java program through makefile.
Honestly I don't really know wether my description is correct....... Cause I don't really know much about makefile... In my assignment, the description is that we must develop a Makefile for GNU make to build our program. For example, the command lines
make
mipsim -v < test1.cmd > test1.log
will build the ISS (a simulator we made) and then run it with debugging output, taking input commands from the file test1.cmd and writing result to test1.log.
I have finished the program but I don't know how to make the things above happen.
What I know so far is just to use makefile to make the .class file from .java file....
I have no idea about how to get test1.cmd as my input file's name and test1.log as my output
file's name from command lines.... I guess these two names probably will get into my program through String[] args in the main function...
Could anybody give me some help please?
Thanks
There is some confusion as to the issues.
First, compile Java using make is a little... iffy. (Most people use ant or maven.) However, if you don't mind a little overhead, you can do it using make. You probably should run make from a directory at the root of the Java package hierarchy. You can determine all Java files below using make macros. Hint: shell:
JAVA_FILES = $(shell find -name \*.java)
Then you run javac. (Make sure to define all path names to compilers etc. using make macros.) With Java, it's not easy to derive a make target, because .class files are not 1:1 w.r.t. java files. I just use a target "compile", depending on all the java files, and touch a file acting as a dummy target.
Second, the execution. To invoke a Java program that is not in an executable jar, you set the classpath (option -cp), specify the main class name and add command line parameters. I'd have to know what "mipsim" is - probably a shell script for doing just that. Anyway, a make target could be the log file:
%.log : %.cmd
${JAVA_HOME}/bin/java -cp ${ROOT} <$< >$#
Now, make test1.cmd should run your program.
Note: Redirection is not specified by program arguments; this is handled by the shell.
Quick comment on your question.
your makefile needs 2 targets. one for build and the other for the run.
all: build run
build:
(this is to build class file from your java source)
run:
put your java command line here like "java ..."
When you run "make", it will call "all" target. And all target will call "build" and "run" target, so just put one thing in one target and use the combinations.
Your java code.
Is your java takes input filename as argument or from stdin?
If you want to take input filename, then you can take it from args argument passed to your main(String[] args).
If you want to read from stdin then you can create a bufferedreader as below.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Hope this help.
(+1 please, if you like this answer)
There are a bunch of unrelated questions..
The syntax you are showing:
mipsim -v < test1.cmd > test1.log
would call an executable mipsim. Pass "-v" to the args[1]. Redirect test1.cmd to be the standrad input and test1.log be the standard output.
The output input redirection happens by the operating system so in c++ reading from std::cin will read from the file and writing to std::cout will write to test1.log
In java these will be redirected to System.in and System.out
About makefiles
Basically a make file rule looks like this:
<target>: <dependency1> .. < dependencyn>
~tab~ command
So just like it is possible to build a target that calls Javac. It is possible to build a target that calls Java.. and so you can build a test target and use it to execute any command you need
If you built a c++ executable then you can execute it from the makefile in the same way.
test: mipsim
mipsim -v < test1.cmd > test1.log
Your final question about pass parameter values to command line from make file do you mean something like this?
make PARA1=1 PARA2=ABC.c
You can use the parameters in your makefile..
test: mipsim
mipsim -v < $(INPUT_FILE) > $(OUTPUT_FILE)
I need to write a bash script to compile my java program. I know it's a bit artificial but it is homework. (although I'm not sure the bash script is even marked, just used for the automated marking system)
I only have one java file test.java and the script is to only search its own directory: I tried:
#!/bin/bash
javac test.java
and saved as build.sh, i tried to run this from the terminal as both sh build.sh and bash build.sh both gave me errors. Can anyone offer any help?
included errors:
build.sh: line 1: {rtf1ansiansicpg1252cocoartf1038cocoasubrtf350: command not found
build.sh: line 2: syntax error near unexpected token `}'
build.sh: line 2: `{\fonttbl\f0\fmodern\fcharset0 Courier;\f1\fswiss\fcharset0 Helvetica;}'
It looks like you've saved your script as rich text (RTF), not plain text. Try re-saving it using your favourite plain-text-friendly editor?
Silly question, but what editor did you use to edit your Java program?
It looks like your Java program is in RTF format and not plain ascii.
If you tell me you used Wordpad, I'm going to track you down, fly to your town, come to your work, and slap you silly. DO NOT EVER USE A NON-PROGRAM EDITOR FOR EDITING A PROGRAM.
Sorry for the all capitalizations. I have a bunch of bozos cow-orkers coworkers at my work who think Wordpad and Notepad have been blessed by God himself as the perfect pair of program editors. At least once per week, I'm fixing problems caused by someone using these programs.
If you're on Windows, download Notepad++. If you want to learn a real program editor, try VIM. If you're on Linux or Mac OS X, you already have VIM installed on your system. If you want a more GUI oriented program editor, try downloading JEdit.
Just make sure you use a true program editor the next time you need to edit a program -- even if it's nothing but a two line shell script.
It seems like the editor you're using adds some extra information to the file:
{\fonttbl\f0\fmodern\fcharset0 Courier;\f1\fswiss\fcharset0
Helvetica;}'
what editor are you using? Try opening the file with something simple, like nano, and remove all the extra stuff.
As part of my python-based program, I have to call a Java program - solved that perfectly with os.system(). It worked well, until I moved the entire thing from one directory to the other. I changed all the filepaths in the python code, and the java program - as best I can tell, I don't really understand java - relies on relative file paths, which weren't changed during the move. But now, the java program won't open. The command line appears then vanishes almost instantly, so I know that os.system() is working. It must be to do with the filepath I'm using in os.system, as when I change that back to the original filepath it works perfectly fine again. Code below:
os.system("java -jar C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
Where fileNames is a variable thingie passed to the java program as an argument, which I'm fairly sure isn't the problem. If I call the python program directly from cmd.exe, then it gives me back the error message "Unable to access Jarfile C:\Documents". I thought this might have to do with the spaces in the filepath, so I put underscores in:
os.system("java -jar C:\\Documents_and_Settings\\enginx_mgr.ISIS\\My_Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
And it gave me the same "Unable to access Jarfile" message, but this time with the full filepath. Trying os.path.exists() on the filepath returns true, so python knows its a real filepath; I guess it must be the command line disagreeing with it, but I don't know why. Any ideas?
Edit: Original filepath, if its of interest, was C:\Inetpub\ftproot\JPivSource\jpivc.jar
Edit 2: Its almost certainly not the filepath, going by the answers below and the fact that none of them work (and that the original filepath works). Checked the security options out of a hunch, and I have full control over the .jar file, as does the system, so its not that it can't access it for security reasons. Still at square zero.
Not a direct answer but ...
I think it is better to call .bat file instead of direct call to java with many command line option. This way you will not need to change Python program to add some other options (like -Xms2048m or -Dfile.encoding=utf8).
Such .bat file is also much easier to debug.
Your problem looks to be caused because of a typo somewhere. This should fix it:
Open Windows Explorer
right click on the file
click "Properties"
copy the location
paste that location into your script, with directories escaped.
You have to put quotes around your path.
os.system('java -jar "C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar" %s' % fileNames)
I'm sorry for offering up another file path solution, but this wasn't mentioned, and it seems likely that the changing of paths would be causing the issue. So, if you wouldn't mind humouring me?
os.system("java -jar C:\\Documents\ and\ Settings\\enginx_mgr.ISIS\\My\ Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
All I've done differently, is escape the spaces with a backslash.
You said that os.path.exists is returning True, and that's fine, but you're trying to execute a command line program by passing it a number of arguments. The program reading the arguments will interpret the string as several strings because of the spaces.
You could also try altering the quotes that you are using:
os.system('java -jar "C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar" %s' % fileNames)
That file path you're using does look quite strange.