Difference between running with Java & Scala [duplicate] - java

Can command java run a compiled scala code? If so, why do we have an exclusive command scala?

You can run byte code generated by Scala if you include all necessary runtime libs for Scala (scala-library.jar, scala-swing.jar ...) in the classpath. The scala command does this automatically, and supports Scala specific command line arguments.

Yes, it can. Scala is compiled down to Java bytecode. But remember that it depends on the Scala runtime classes, so you need to still have Scala's jar files on the classpath.
If so, why do we have an exclusive command scala?
Convenience wrapper.

Scala is designed to integrate easily
with applications that run on modern
virtual machines, primarily the Java
virtual machine (JVM). The main Scala
compiler, scalac, generates Java class
files that can be run on the JVM.
-> http://www.artima.com/scalazine/articles/steps.html
As long as you have installed the scala runtime you should be fine: compile classes with scalac and run them with java.

Just want to add my own answer as additional value for the future readers:
scala, if run without parameter, will run an interactive shell
scala, if run with a text file name as parameter, will regard the file as a scala script
those two can't be done using java

If you look closely, the scala command is simply a bash helper-script which summarize to the below command:
$cat /usr/local/Cellar/scala#2.11/2.11.12_1/libexec/bin/scala
execCommand \
"${JAVACMD:=java}" \
$JAVA_OPTS \
"${java_args[#]}" \
"${classpath_args[#]}" \
-Dscala.home="$SCALA_HOME" \
$OVERRIDE_USEJAVACP \
"$EMACS_OPT" \
$WINDOWS_OPT \
scala.tools.nsc.MainGenericRunner "$#"
There are 2 things required to run a .class file compiled using scalac ( the scala compiler) using the java command.
We need to include the scala-library.jar and the location of the .class file in the classpath. To find the location of scala-library.jar, please execute the
below:
which scala /usr/local/opt/scala#2.11/bin/scala
In my case the scala-*.jar files are in :
/usr/local/Cellar/scala#2.11/2.11.12_1/idea/lib on Mac
The location of the Main2.class file which is in /training/example1/scala.
So, to execute the program we could use the below command:
java -cp /usr/local/Cellar/scala#2.11/2.11.12_1/idea/lib/scala-library.jar:/training/example1/scala/ Main2
EDIT-1: If you are using windows, please use semicolon(;) as the separator in java classpath command.

Related

What the reason for Kotlin to create jar file execute a class?

Despite the fact that both java and Kotlin share the same JVM and same compiler. I can see the only Kotlin always need a jar to execute the file but not java
Kotlin
kotlinc hello.kt -include-runtime -d hello.jar
kotlinc hello.kt -d hello.jar
Java
javac hello.java
java hello
Can some one explain me why?
Executable Kotlin code need its runtime (packaged as a JAR) to do so, since it doesn't compile down directly to native Java (i.e. exclusive of all non-standard JRE libraries) code as javac (with no arguments) would do
If you did have a native .class file from Kotlin, you'd probably still need to run java -cp kotlin-<something>.jar hello, where <something> could be stdlib or runtime

Access classes defined in a jar file, from within (for instance) JRuby irb?

(Crossposting note: I have asked this question one week ago at the JRuby mailing list, but didn't get any answer yet).
I have a jar file provided by someone else, no access to the source code. The jar file is in lib/other/appl.jar, the class is named Appl, and the package is com.abc.xyz
I would like to instantiate an Appl object from the JRuby irb, jirb_swing_ex.
(Of course my problem applies not only to jirb, but to running JRuby programs in general, but I explain it in the way I'm using it right now, just in case there are some peculiarities in Jirb which need special treatment).
THIS is the way it DOES work:
(1) Invoke jirb by:
java -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
(2) Put the directory with the jar file into the load path:
$: << 'lib/other'
(3) Load the jar file
require 'appl.jar'
(4) Import the class
java_import com.abc.xyz.Appl
(5) Create the object
x = Appl.new
As I said, this works, and I can live with it if necessary, but I would prefer a simpler approach:
NOW TO MY QUESTION: Instead of fiddling around with load path and doing a require for the Jar file, I thought I could let Java already include the jar file. This is what I have tried:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
The problem is: How can I get at my object? If I just use the class name com.abc.xyz.Appl, JRuby complains that the class not found (NameError: missing class name).
BTW, I have also tried forward slashes (since I'm on Windows), i.e.
java -cp lib\other\appl.jar -jar jr\jruby-complete-1.7.27 jb\jirb_swing_ex
but the same effect. I had expected that, having appl.jar in my class path, would make the classes available somehow, but I seem to miss something.
Running jirb or jirb_swing with custom class path
jirb and jirb_swing will use the value of JRUBY_CP environment variable (if present) to extend class path given to Java command line.
Example with commons-lang3 library taken from my local maven repository, using bash on Linux or macOS:
$ export JRUBY_CP=${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar
$ jirb
irb(main):001:0> org.apache.commons.lang3.mutable.MutableBoolean.new
=> #<Java::OrgApacheCommonsLang3Mutable::MutableBoolean:0x7c24b813>
Running JRuby programs with custom class path
To run a JRuby program that uses a third-party java library, this won't work:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 ...
You must use either -jar or -cp, you can't combine the two.
From java man page:
When you use this option [-jar], the JAR file is the source of all user classes, and other user class path settings are ignored.
In addition, you need to pass the main Java class, which is org.jruby.Main, and that class needs arguments: either a path to a Ruby script, or other command line arguments such as -e 'puts 2+2'.
So the command line structure is the following:
# Run script file:
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main path/to/script
# Run simple one-line Ruby program
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main -e 'some ruby here'
(on Windows please use ; instead of : as separator)
Actual example with same commons-lang3 library & OS:
$ alias myjruby="java -cp ${JRUBY_HOME}/lib/jruby.jar:${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar org.jruby.Main"
# Verifying base jruby code works with that:
$ myjruby -e 'puts 2+2'
4
# Now verifying it can use my 3rd-party lib:
$ myjruby -e 'puts org.apache.commons.lang3.mutable.MutableBoolean.new'
false

Compiling NanoVM Java Virtual Machine

I have problem with compiling NanoVM project from:
http://www.harbaum.org/till/nanovm/index.shtml
NanoVM is Java implementation for AVR microcontrollers. I know this is not efficient. I have problem with "makefile" file. This makefile contains instructions to compile Java .class from source .java files. But i don't know, how can i run makefile? I was using previously javac.exe compiler to write java standard applications. In install instructions writes that I must have Java SDK (J2SE). Is there any difference between SDK and JDK? I have JDK installed, i have read earlier that there isn't any difference between SDK and JDK, but i'm not sure. I know makefile files are used to manage compilation, i previously used makefile to compile programs wrote in C, but is there some Java compiler like gcc? Sorry for my stupid question, i'm quite newbie in makefiles, i think solution is very simple.
My makefile:
#
# Makefile for NanoVMTool
#
APP = NanoVMTool
VERSION = 1.5
all: ../$(APP).jar
CLASSPATH = ../../java/examples
NATIVEPATH = ../../java/native
JAVAFILES = AccessFlags.java CodeInfo.java ConstPoolEntry.java FieldInfo.java \
MethodInfo.java AttributeInfo.java CodeTranslator.java \
ConstPoolEntryError.java InnerClassInfo.java NanoVMTool.java \
ClassFileReader.java CommonInfo.java ConvertException.java \
LineNumberInfo.java NativeMapper.java ClassInfo.java \
Config.java Debug.java LocalVariableInfo.java UVMWriter.java \
ClassLoader.java ConstPool.java ExceptionInfo.java \
MethodIdTable.java Uploader.java NVMComm2.java
# compile target code
$(CLASSPATH)/%.class: $(CLASSPATH)/%.java
javac -classpath $(CLASSPATH):$(NATIVEPATH) $<
%.class: %.java
echo "public class Version {" > Version.java
echo " public static String version = \"V$(VERSION)\";" >> Version.java
echo "}" >> Version.java
javac $<
../$(APP).jar: $(APP).class
jar cmf $(APP).mf ../$(APP).jar *.class
# convert and upload a class file (should be moved to vm/target Makefile)
asuro-%: $(CLASSPATH)/%.class $(APP).class
java $(APP) ../config/Asuro.config $(CLASSPATH) $*
mega8-%: $(CLASSPATH)/%.class $(APP).class
java $(APP) ../config/Mega8.config $(CLASSPATH) $*
clean:
rm -f *.class *~
In order to run a Makefile (on Linux / UNIX), you first need to install "make" and any of the other tools that it uses. In this case, the tools are just the java, javac and jar commands. For the last two, you need a JDK installation.
But i don't know, how can i run makefile?
Change directory to the directory containing the Makefile, then run 'make' with the appropriate target. In this case make all.
Is there any difference between SDK and JDK?
There is no such thing as the Java SDK.
... but is there some Java compiler like gcc?
The Java compiler is javac. That is what your makefile is using.
Sorry for my stupid question, i'm quite newbie in makefiles, i think solution is very simple.
The REAL solution is to find and read a tutorial on Makefiles and how to read, write and use them. (In general, the solution to asking newbie questions is to educate yourself so that you aren't a newbie!)

Using Command Prompt to execute a java file

The problem I'm currently having is that I am trying to execute a java file using command prompt, I understand the PATH being set to the jdk's file. Though my java file contains libraries and has to import the libraries, how would I ' import the libraries ' when it runs?
Sample command :
javac ClassName.java 1 1 1
When it executes it errors on the imports so what should I do?
The .jar files are Java "libraries". What you need is something like:
> javac ClassName.java
> java -cp Library1.jar:Library2.jar ClassName.class
The first line (javac) compiles the Java code into a class file. The second line runs the compiled class. The '-cp' option sets the CLASSPATH (makes the code in the jar files available at runtime). Note: the exact syntax will depend on if you are using Mac OSX/Linux or Windows. Windows uses the ';' character to separate the jar file names.
Several issues here:
Are you compiling or executing? javac is the compiler. java is the runtime virtual machine.
ClassName.java is source. ClassName.class is the compiled bytecode run by java.
As a general rule, your environment should contain the variable JAVA_HOME, and that should point at the directory where the JDK resides on your computer.

creating 100% standalone executable jar that doesn't require the java command

so apparently if you create an executable jar, in order to run it you still need the java command:
java -jar something.jar
but what if I just want it to run without the java command, so just directly from the command line
something.jar
is there a way to export my java app in eclipse in order to accomplish such
On Unix systems you can append the jar file at the end of an executable script.
On Windows you have to create a batch file.
For instance in Unix:
$cat HelloWorld.java
public class HelloWorld {
public static void main( String ... args ) {
System.out.println("Hola mundo!");
}
}
$cat M.mf
Main-Class: HelloWorld
$cat hello
#!/bin/sh
exec java -jar $0 "$#"
$javac HelloWorld.java
$jar -cmf M.mf hello.jar HelloWorld.class
$cat hello.jar >> hello
$chmod +x hello
$./hello
Hola mundo!
In windows you have to create a batch file like:
::hello.cmd
javaw -jar hello.jar
Which has the same effect.
On Windows and OSX you can double click on the jar to run it, I'm pretty sure you may add a trigger on Linux too.
I hope this help
Excelsior JET - http://www.excelsior-usa.com/jet.html - claims to compile to native code and bring its own runtime support, so it does not require an existing JVM. Commercial product.
I have not tried it myself, but they have spent quite a bit of effort over the years to market JET as a great deployment method for precompiled binaries.
Also note that if you have an executable/runnable jar which works fine with "java -jar someting.jar" and you just want to be able to invoke it in a more convenient way, this is the job of the program accepting your command and launching the java command.
For Linux you can frequently add an alias saying that "something" expands to "java -jar something.jar", and some command interpreters allow for saying that all commands ending with jars should be executed specially. The exact details depend on which shell (command line interpreter) you are using.
What you need is a tool called 'Java Executable Wrapper'.You can use it to Pack all your class files to a Single Executable Package.
The One i recomment is launch4j,you can download it from sourceforge launch4j.sourceforge.net
Launch4J can be used to create standalone Executables (.exe) from a jar file for windows Environment.
The thing is, that Java gets interpreted by the JVM, so you'll at least need to ship it with your app.
To be a little more specific about this, Java gets kind of compiled to byte-code so it can be interpreted faster. But the Byte-Code can't run without the JVM. This is the nice side of Java: You don't need to recompile your Apps to run on other platforms like Linux or OS X, the JVM takes care of that (as it is written in native code and is recompiled for those platforms).
There are some compilers out there which can convert your Java code to something native like C which can then be executed without the JVM. But this isn't the idea behind Java and most of those tools suck at what they do.
If you want your App to run without any interpreter, you'll need to use a compiled language like C or C++
Java program runs on a JVM, for the first question I don't think there's a compiler that can do the job well. For the second question since a jar file is not an executable per se, there must be some sort of settings in the target machine, "executing" a jar file without providing the java command is a matter of convenience for the user. On Windows every file extension has a program associated with it, so .doc documents have (usually) Word as the program associated -that setting is set by the office installer, the java runtime also sets the setting for .jar files when you install it, but behind the scenes, java command will be used by the system. So the short answer to the second question is: depends on the target machine.

Categories

Resources