Problems compiling file on School's Unix System - java

COMPLETE EDIT BUT SIMILAR PROBLEM
What's the best software/plugin to enable FTP on Eclipse? I'm using FileZilla, but is there something better/easier?

You are telling javac to compile gamedata.txt and it is reporting an error that it cannot compile this file.
I'd highly suggest using a tool like Ant to script your compilation/packaging/etc so you don't have to worry about typing in arguments on the command line.

First of all, the -J command line argument is not meant to be literally passed as -J<flag>. Taken directly from the javac man page (you can view the exact same thing by typing man javac into the shell):
-Joption
Pass option to the java launcher called by javac. For example,
-J-Xms48m sets the startup memory to 48 megabytes. Although it
does not begin with -X, it is not a `standard option' of javac.
It is a common convention for -J to pass options to the underly-
ing VM executing applications written in Java.
Really, if you want to make this an executable, you can just use the tools that exist in Eclipse to make an executable. Using the command-line javac adds an extra level of complexity that is unnecessary, and that Eclipse is specifically designed to remove.
In eclipse, you can (I think) use File->Export->Java->Executable JAR File to make your project into an executable JAR that any computer with the Java Virtual Machine can run. That way, your project will work on both your computer and the Unix system at your school. You may have to add gameData.txt manually to the JAR or include it separately in the package, not sure how Eclipse does that type of thing though.

You can only compile .java files. If you remove the .txt file from the list of files to compile, it should work fine. If you want to compile all the files in a directory, you can simply use javac *.java

There are some examples in the javac synopsis.
Edit: Updated link to Solaris examples, which are similar to Linux.

Related

How do I create a Unix load card for a java program

In Unix (or Linux), if I want to run a shell script, I can start the file with #!/bin/sh. With awk, I start the executable file with #!/usr/bin/awk -f and it treats the rest of the file as the program.
How do I do that with a Java program? I tried copying the simple.class to simple, putting #!/export/appl/Mail/java/bin/java at the top and making the file executable, but I get:
69> ./simple
Error: Could not find or load main class ..simple
I know this can be done with an executable shell script, or a C program that execs the java interpreter. Every other interpreter on Unix can be called with a #! load card, surely there's a way to do it with Java.
The most usual way is to have a wrapper for the Java. A shell script that executes the "java -jar yourJar.jar" or equivalent. And then you bundle the shell script and the windows equivalent bat file with your product.
Another option is to have a native launcher. For example you can see the Eclipse project which has gone that way. You download Eclipse and you have a native executable to run. The native executable will launch your Java program.
One more option is to compile Java into native code. For example you can use this commercial tool called Excelsior JET ( http://www.excelsior-usa.com/jet.html ).
The Java class file format doesn't allow text before the header, that's why the Java runtime no longer accepts the .class file after your modification.
On Linux you can use binfmt_misc to support additional executable formats, including .class files. It's basically a way to tell the Linux kernel how to detect executable formats and how to execute them.
This Archilinux Wiki article explains in more detail how to get this effect.
You cannot do it with a Java program. Firstly, the Java program needs to be compiled before execution. Secondly, even if compilation wasn't required, the hash sign is not a comment in Java, so that would be a syntax error.
I've never heard the term "load card". What you have is an "interpreter directive" designated by a shebang. This merely designates which interpreter the shell should invoke on a given script.
As for why C programs can be run directly in the shell, executables recognized by the operating system are passed to the loader. A Java class isn't an executable, at least to the OS anyway. So the shell must know which interpreter to pass control to.
But as you've noticed, the shebang doesn't work. The reason is that the class file is in a specific binary format that the JVM expects. Editing this file will break convention and lead to an error. Therefore, there is no way to do what you've asked.
However, you can create a "shortcut" to the program you want to run by creating an alias or perhaps writing a one-line Shell script to wrap the java command you need. This is the common practice as I understand it.
The other answers explain why you can't do what you are trying to do. However, if your shell is zsh, you can create a suffix alias. For example, to execute .jar files using java:
alias -s .jar="/usr/bin/java -jar"
Then, to execute blarg.jar, you just type ./blarg.jar. Of course, you must chmod +x your .jar file first.
Apart from the wrapper script and binfmt_misc solutions suggested by others, I'd like to suggest a potential solution which doesn't directly answer your question but maybe it solves your actual problem.
Since Scala does have an interpreter that can run code without you having to compile it first, and this code can reference any Java code, if your goal can be summed up as "using Java as a shell scripting language", you could use a .scala file as your starter script (which can include the shebang to be run with scala) from which you can call all your Java classes. This isn't any simpler tha having a bash-based starter script, but it's a good starting point to gradually move to scripting in Scala instead of Java in which case you can get rid of the need to compile to .class file in the first place.
The reason this doesn't work is that Java isn't really an interpreted language, it's partially compiled, partially interpreted.
The .java source code that you'd put the hashbang directive in needs to be compiled to a .class file before the java interpreter can run it. Comments are stripped out by the compiler, so there's no way to push a comment from the .java into the .class file. The .class file is "compiled" output in a specific format, so adding a hashbang directive to the top of it would break the format.
Java isn't really meant to be a scripting language - but some JVM languages are. For example Groovy supports hashbang and so does Clojure.

Have a difficulty with installing a .jar library

I have just downloaded a third party java library which i need for a program i'm about to create.
But i can't figure out how to actually install the library so that i can literally type
import path.to.library;
in my java class file without having any errors.
I have looked at many tutorials and answers on StackOverflow but each of them seems to include the use of some or the other IDEs for java.
Well, i'm a bit rustic and would like to know how to make it work with notepad and the command line, coz that's what i use to make a program.
When you are compiling, include the following in your line:
-classpath nameOfJar.jar
However, once you actually switch to use an IDE, you will see the multiple benefits this approach can bring.
You don't specify a path in the import statement, just the package name.
All usable JAR files have to be specified in the classpath on commandline when starting your Java program.
You need to understand how Java's classpath works. For a comprehensive description, read the Oracle manual page on this topic. Alternatively the PATH and CLASSPATH page of the Java Tutorial.
(FWIW - it is generally considered to be a bad idea to use the CLASSPATH environment variable to set the classpath, because this is liable to lead to "nasty surprises" if you deal with software that requires different classpaths.)
If you don't use any IDE you won't have code complete. However if you know all the packages/classes/methods names/signitures you can use pure Notepad and then compile it by adding the library to your classpath (eg. using the -cp switch in the javac command when compiling)
JARs are not required to be installed. They required to be accessible at compile- and run- time. You can add jar by command line parameter or CLASSPATH environment variable. IDEs have special means for setting JAR;s location in visual manner.
You'll need to be more specific about what you have tried so far. This generally isn't something complex though, if you want to manually invoke the compiler you would do something like
javac -cp somejar.jar myclass
Once you get used to this process, it's better to automate it using a build tool such as ant or maven. Ant is a little easier to begin with, maven has some additional capabilities that make it a little more complex.

Trouble when trying to compile a .java file through javac

When trying to run the following command to compile some_file.java in Windows
javac -classpath "some_class_path" "some_file.java"
it fails, telling me I didn't pass it any source files.
If instead I make
cd "some_class_path"
javac -classpath "some_class_path" "some_file.java"
it works fine. Why? This bothers me as I want to compile a set of .java files from my program through javac.
It would help if you could be more concrete about your description, but it looks like you're expecting the classpath to be used to look for the files you specify on the command-line as well. It doesn't work that way - the source files you specify must be the exact paths to those files.
Try to use full path to java source file. Anyway, could you provide exact commands and exceptions?
This is not the usage of the classpath. The classpath option specifies where to find the dependencies, not the source files. Just compile using:
javac "path\file"
(windows version)
I don't think I see any obvious reason why that wouldn't work but then work if you're in that directory. Maybe quotes don't work the way I think they do in Windows. Either way, I suggest that you look at Ant, as it is the industry standard way to compile a set of java files.

Compiling Java package throws errors for external Jars

Pretty basic problem here. So I have a Java package that I have created that has three classes (one has the main method). I am trying to use a few Apache Jars, and have added these to my build path in Eclipse. However Eclipse wont let me build and run it properly, so I am trying the command line. I have added the env var CLASSPATH and pointed it to my lib directory which hold the Apache Jars. However, when I try to use javac I get a bunch of errors:
package org.apache.xmlrpc does not exist
import org.apache.xmlrpc.client.XmlRpcClient;
I was reading the man page for javac and it said that:
If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
So I tried copying the Jars to the same location as my three source files, but no change.
Can someone please tell me what I'm doing wrong?
Thanks.
Classpath variable (or command line option of javac) must contain all jars explicitly. It cannot go through jar files stored in specified directory.
You can compile this by specifying the option -cp on the command line:
javac -cp foo.jar:bar.jar foo/bar/Baz.java
You then run it with the same option:
java -cp foo.jar:bar.jar foo.bar.Baz
It sounds like you've just set the classpath to the directory containing the jar files. You need to set it to the individual jar files, or use java.ext.dirs to set an "extension" directory containing jar files. I'd recommend using the specific jar files. Something like:
// Assuming Windows...
CLASSPATH = c:\libs\foo.jar;c:\libs\bar.jar
I'd also personally recommend specifying the classpath on the command line instead of using an environment variable - the latter will work, but it ends up being a bit more fiddly if you want to compile different projects against different libraries.
However, I'd actually recommend getting Eclipse working first, rather than retreating to the command line. It should be fine - if you could give us more information about what's failing in Eclipse, we may be able to help you with that instead.
The jar files in the current directory are not automatically included; that only refers to .class files in normal package/directory hierarchy. Jar files must be added either explicitly, or via a wildcard like javac -cp ./* (Assuming JDK6+)
(Some OSes may require an escape of the * to avoid globbing; OSX does not.)
I agree with previous answers, but I would also recommend to use proper java build tool - like ant (perceived easier to use, but not necessary) or maven ( perceived more difficult to use, but really worth learning )

Java command-line launchers

I am working on a .jar file library to implement a bunch of helper classes to interface a PC to a piece of external hardware. I'll also add some simple applications, either command-line or GUI, to handle common tasks using the library.
My question is, is there a recommended way to simplify the command-line instantiation of a JVM in a certain specific way?
e.g. instead of requiring a user to type a cryptic error-prone command like:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar arg1 arg2
instead I do
TurboBlenderApp arg1 arg2
?
I suppose I could use shell scripts (incl. win32 Batch Files... gacckkk), it's just that I'm not good at those + I was wondering if there was a more straightforward platform-independent way of running a Java app from the commandline.
When you use -jar, then-cp (and the CLASSPATH variable) will be ignored
Just provide a executable jar. "java -jar TheApp <whateverargumentsyouwant>" shouldn't be too hard (you can have a Class-Path attribute in your jar files manifest, however).
if it is too hard write a GUI
or provide those shell scripts/batch files. Writing those isn't too hard either.
If you just want to simplify local execution, use a batch file or even just create a custom shortcut. If you want to create a launcher and package the executable jar and required libs for deployment, use something like Launch4j.
For some reason this trick just doesn't get around. What you need to do is to create a custom manifest for your jar file that defines the Main-class: property, and include in the jar file all the class files you're using. Then all you need is to run
$ java -jar myapp.jar
On real operating systems, you can even just run the jar file, as they will use the magic number to start it. But if you must, a one-liner batch or shell-script containing that line is all that's needed.
This is described in one of the Java Tutorials.
Shell scripts and batch files are the standard way of accomplishing what you want. (Look at any major Java product.)
This is, of course, absolutely pathetic and ridiculous. Go Java.
Your alternative is to write a little C program that does what you need (creates the java process) and compile that for each of your supported platforms. (C is the truly platform independent language).
Either way, you will need to take platform-dependent steps to make your app feel truly at home in the OS. On OS X, you will need to make a .app bundle, on Windows you need to package atleast the icon and version info into an EXE. For Linux, well, shell scripts suffice. :-)
If you use java -jar to launch your application, you can add additional jars to the classpath by adding a Class-Path entry to the main jar's manifest. See the jar file spec for more information.
Another solution is found in the Jakarta Commons, as always : commons-launcher.
A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.
It's pretty easy:
Write this in TurboBlenderApp.cmd:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar %1 %2
// Bear in mind saua answer about -cp and -jar
Then from the command line you just write:
TurboBlenderApp arg1 arg2
Just the way you wanted.
If in Unix/Linux replace th4e %1 %2 with $1 $2 and make sure your app has execute rights ( If you don't know how to do this, I guess you don't need it either )

Categories