bash script compile java - java

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.

Related

gvim/vim: launching from within Java program with parameters

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 ":".

ANTLR Warning: TestRig moved to org.antlr.v4.gui.TestRig

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.

Simple way to run java classfile (Terminal)

I wanted to put a java classfile up for download recently, which people could run in Terminal. It's a Minecraft command generator, so the people downloading it won't necessarily have the greatest mental capacity (I'm referring to 8-year-olds who have no idea what they're doing, of course).
Anyway, I wanted to provide a simple, single command, both for the Mac / Linux terminal and the Windows command line, that ran the classfile without any complications. The problem is, I don't want to execute it by doing /cd path, and then doing java someFolder.someClass. I just want to have a single command to open the file. If anyone could provide these commands for me, both in Mac / Linux and Windows, that would be great.
Sorry for the super long explanation :P
A jar file with a main class in the manifest would probably be the easiest thing. Then the command is java -jar myjarfile.jar.
A swing application would probably be easier as the default way of running executable jars doesn't open a command prompt (it uses javaw instead of java).
You will have to first start a terminal and then run java in that terminal, which can be a bit tricky.
How to open a command terminal in Linux?
Why not create an interface (Swing) and pack everything in a jar?

Strange error calling java program from python via command line

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.

Product .exe does not start if # is in folder name

I was wondering if there is a specific reason, why my eclipse product .exe does not start, if the parent directory of the exe-file contains a #.
I am using Windows XP (SP3) and exported my eclipse application. The resulting .exe file saluts me with the following screen:
(source: iachelini.de)
Any ideas?
Just a guess.
The launcher is implemented in a way that the command line options are interpreted by a batch script ... and the # is interpreted as the beginning of a line comment.
Edit:
The 'eclipse.exe' loads the 'eclipse.ini'. Here you can make comments with a # at the beginning of the line. This does not prove that eclipse uses a batch script for loading (to be sure one should look at the source), but it is an hint that the comment-assumption is valid.
My first guess was wrong. The problem has nothing to do with comments or a batch script. It's a bug in the java code of eclipse. I have filled a bug report:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=320732

Categories