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
Related
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
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.
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.
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.
I have a java project in eclipse with different packages. How can I compile the project from command line in unix (bash) ? Using plain javac doesnt seem to work. For eg, in eclipse I have
src
...server
...client
...shared
And the main file that I want to run later is in the server package called server.java
You will have to add all the source paths and dependency .jar files into the compilation call. The easiest way to do this will be to get Eclipse to export an Ant build.xml for the final jar you are trying to compile. This autogenerated ant file is usually pretty messy but gives you an idea of how the project should be built.
javac -sourcepath /path/to/src -d /path/to/classes