How can I compile Scala code without using scalac - java

Let's suppose I have a file called Main.scala that I want to compile and run.
In a normal environment I can just do scala Main which automatically performs compilation and runs the file.
Now I am trying to deploy scala source code to another Server, on which I am not allowed to install scala. It is just possible to copy files (such as scala-library.jar and scala-compiler.jar) and Java 1.6 is pre-installed.
It would of course be possible to compile to bytecode locally using scalac Main.scala and execute the resulting .class-files using java -cp [...] Main.
For my workflow it would, however, be better if I could just checkout and compile the scala sources on the remote server directly.
Is there any possiblity to realize this compilation task with the tools available or by copying additional binary/jar files?

I would strongly suggest that you use a build tool. Many open source projects use sbt (http://www.scala-sbt.org/). You can use an sbt plugin (https://github.com/sbt/sbt-assembly) to create a fat jar with all your dependencies.
You can also use Maven and Gradle.

Related

Compiling Java sources with Clojure deps.edn

How do I configure my Clojure CLI deps.edn file to compile Java source files along with my .clj Clojure source files?
I found badigeon which can compile Java sources, but I'm struggling to use in my simple project.
You can now do that with clojure.tools.build
Without knowing more about your context ...
The "best" way to do this currently, is to separate your Java and Clojure sources into two projects. In the Java project, create a jar with the compiled classes (using standard Java tools - perhaps make and javac) and install the jar into your local maven repo. Then add the jar file as a dependency in the deps.edn of your Clojure project.
You might also be able to use the jar directly, without installing locally, by using the local/root feature in deps.edn. See the Deps Rationale for how to do that.
Another way would be to arrange to have the Java sources compiled into a classes sub-directory of your combined (Java / Clojure) project and then add the classes directory to the :paths key in your deps.edn. The creation of the class files will (again) have to be done with Java tools or shell scripts - outside of clj.
There are some recent developments in clj that may make this easier in the future.
clj will not do that.
lein do a lot of things that are out-of-scope for clj command line.
You may find some library that do that, then you can call clj -m lib-that-compiles-java

run java file in linux command with maven dependencies

I have written a Java program with intellij and it's working, but now I want to run this file with a command, but when I try to compile it with javac I get errors because it doesn't recognise my imports, I'm new to programming so how can I run my program knowing that it has dependencies in maven?
If you have a choice, you should use maven command (mvn) rather than the low-level compiler javac, it makes compiling and packaging much easier.
But if you want to use javac, you must pass the path to the jar your code depends on, as explained in : How do I compile a java file that has jar dependencies?
If you have pulled your dependencies using maven and you are using centos, the jars should be stored in directory ~/.m2
But once again you should consider installing and using maven, if you want to manually compile your code in a similar way Intellij does
Maven was created to make it easier to handle builds with dependencies (jar files) which can be automatically downloaded from the internet. This includes invoking javac with all the dependencies on the build classpath using "mvn compile" - you do not have to invoke javac yourself! Look in target/classes for your byte code files compiled by Maven.
You can also ask Maven to run your Java class with the required dependencies available. Use the following command (adapted as necessary):
mvn exec:java -Dexec.mainClass="com.example.Main"
See http://www.mojohaus.org/exec-maven-plugin/usage.html for more details.

Java classpath in VS code

In a java project I want to compile classes using Vs code.
The project is not Gradle nor maven based.
The compilation requires classpath (i.e. some lib jar files).
I want to compile whichever the class I am currently working on (not all as there are 100s of classes and I do not want them to update each time). e.g.
$javac -cp c.jar HelloWorld.java
How can I set classpath in Vs code to accomplish this task?
YOu sould try using java.classPath https://code.visualstudio.com/docs/java/java-debugging#_configuration

How to build ceylon using the ceylon commandline tools?

How can I setup and build the ceylon distribution as a mixed java and ceylon project using only the ceylon commandline tools instead of ant?
I assume having cloned the git repository, cleaned and dist-ed the ant project following the instructions in https://github.com/ceylon/ceylon, ceylon/dist/README.md, and therefore - among all the other parts - having ready a parser from the grammar using antlr.
Cant you outline the necessary steps, or show a script that collects the required .java and .ceylon files and the resources and .xml files and so on from the git / ant project. Cant you setup one on git hub?
Specificly, what dependencies am I to put where (repo), and how must I handle version strings like ¨1.2.1¨ when working with java dependencies (jars, not cars) in the ceylon project?
For the sake of simplicity I could do with building only the compiler-java and related subprojects, specificly the typechecker, leaving away the compiler-js and its related .js files.
Finally, why is the ceylon distribution built with ant instead of the ceylon tools?
I can only partially answer my own question as far as the the build of the ceylon typechecker is concerned. After generating all the necessary java source files and consolidating the source directories the typechecker was built without errors and could be used without runtime errors.
Speaking for myself this is enough to discard the hypothesis that compiling a java project is an enterprise that never leads to success. In this considerable instance it even led to success at runtime. This is truly amazing.

Compiling without having to put project files under classpath

I want someone to be able to compile the code without then having to set the CLASSPATH to the project directory. Is this possible?
You can set the classpath to be used for the compilation as a parameter passed to the compiler instead of setting an environment variable. The parameter is -classpath or -cp.
The classpath needs to be set somehow, though using the environment variable is not recommended these days. Typically, projects that people are supposed to compile for themselves are distributed with either an Apache Ant build script or a Maven POM descriptor. Both of these build automation systems can do much more than just compilation, but both require some time to learn if you haven't used them before. However, it is time well spent, as they can save you a lot of time on all kinds of repetitive tasks, and both are very commonly used in Java projects.
Use a build tool, like Ant or Maven. They use a project descriptor of one sort or another to manage the classpath for you.
Sounds like you want to create a .jar
The jar tool in the JDK puts your .class files in an archive. Then you can just reference the archive on the classpath or with the -jar flag for the java executable.
If you create a manifest for the jar, you can make the .jar runnable with a click from Windows and most other desktop environments.

Categories