Compile java class in VSCode - java

I'm writing a code to run the original coded program from my professor through the class files. So basically I downloaded my coded program of my professor to start coding a class file in the visual studio code. But when I run, it could not find my class file which means that I haven't compiled it, then I have searched up some ways to compile the file which has the javac or some like that and it turned out:
"'javac' is not recognized as an internal or external command
operable program or batch file.
The system cannot find the path specified."
So how can I easily compile the class file and set the classpath?

Visual Studio Code is just a code editor. VS Code can't actually compile your code: Only help you write it. Various extentions to VS Code can automate the process of running the compiler, but you still need to install the compiler yourself.
To compile your code you'll need to install the Java Compiler (javac) which is a separate program. You can install the Java Compiler by installing the Java Development Kit, which is a set of tool for developing Java programs.
Typically the Java Develoment Kit (JDK) instillation will set up your operating systems paths so javac will refer to a valid program after your restart VS Code and your consoles. If it still doesn't, refer to the answer linked my MikaelF's answer. You'll need to manually tell your computer where you installed the JDK.

only jdk can help you to compile java class;
open cmd type:"java -version"; if not have proper response(the version of jdk),you should download jdk from oracle and install it,and ofcourse add the bin directory like this(D:\workSoft\JDK8\bin) to the path environment;
if you had install the jdk correctly then visual studio code will compile the .java file to .class file automatically

Related

VS Code runs .java files without the producing .class files

I have some basic java knowledge and i decided to switch from Intellij to VS Code. I know how to compile and run a java file (which may use other imported .java files) from the terminal, but i want to run a java program using the run icon inside vs code.
I'm not a 100% sure, but i think that in order to run a java program inside vs code you need a extension. For this reason i downloaded the Java extension pack. Note that i already have installed the latest jdk on my computer.
After i installed the plugin, i could run the program using the "run without debugging"/clicking in the run icon. However, no .class files are produced. I control+h but there isnt any hidden folder that vs code may drop the .class files.
How does the program run in the first place?
How can i config vs code in order to compile the java files before running the one that has the main function?
Basically, when you run a java file normally, you compile it with javac and then run it with java (or something along those lines, there are many different variations of possible ways to do it). If you go ahead and try, it is possible to run java filename.java and skip the normal javac step. In the case of VSCode, it will differ based on what extensions you have, but some may run the code with a simple java filename.java and others may have different settings set. If you take a look at your extension preferences as well as your preferences for java by searching your settings, there may be an option, such as "Java Source Paths" enabled which changed where compiled files are saved by VSCode.
If your project is a folder containing Java source files. And no build tools(Maven/Gradle) are used.
If that is the case, you can trigger the command Java: Configure Classpath, then find the section output and then set a relative path to your workspace.
The output files are by default stored inside the workspace storage.
If after triggering the command, what you see is the native vs code setting page. Then try to search java.project.outputPath and then set a relative path to it.

How to compile Java class with .jar dependencies on Linux command line

I'm a CS Student working with my first .jar file and I'm having trouble getting my class that's dependent on it to compile. I'm using VSCode as my code editor, and I've added the .jar file to the "Referenced Libraries" section of my java project. The class I'm trying to run is a fairly simple class generated from our textbook, with an accompanying .jar file that has a bunch of custom libraries on it. I can see the contents of the jar in the VSCode java project browser, but when I try to compile I get import and symbol not found errors. I usually compile and run my projects in the terminal window (running Ubuntu 18.04 over Windows using WSL) by simply typing "javac MyClass.java". I would expect that if the jar has been loaded into VSCode, then it should compile the same way, but that doesn't appear to be the case. I've tried the recommended syntax "javac -cp /lib/myjar.jar MyClass.java" and "javac -jar /lib/myjar.jar MyClass.java" but neither works for me.
I'm fairly certain there's something simple I'm missing, as I don't have any experience working with jars, and certainly not in VSCode. Anyone out there that has an idea what I'm missing?

How does importing libraries in Java work

I'm starting to program in Java and I can't quite understand how importing libraries works.
Suppose I need to import a library in order to be able to write a specific program. I save it in my PC.
How is someone who does not have the same library on their machine supposed to be able to run the program? This is probably a silly question, but I really am new to programming.. Thanks.
They need a java runtime (JRE) installed.
Third party libraries like apache commons, accompany your application jar.
myapp.jar
lib/apache-commons.jar
lib/ido.jar
The myapp.jar (zip format) contains a METAINF/MANIFEST.MF containing an entry
Main-Class: ...
Class-Path: lib/apache-commons.jar, lib/ido.jar
And with that myapp.jar is an executable jar.
A build tool like maven can be used to build such a distribution: take care of library versions (dependencies), preparing the manifest, generate to output folder.
"Someone who does not have the same library on their machine" is a wrong concept. A machine has to have the Java Runtime Environment (JRE) installed on it in order to run a Java program (i.e., to execute the .class file). The JRE contains all the Java libraries. Therefore, every machine running Java has every library that you can possibly use in your program.
Importing a library in your program is basically instructing the compiler to include that library in the program. You don't really save the Java program on your PC with the libraries in it. It's just a set of instructions. These instructions are given to the Java platform on any machine the program is run on. While running the program, the JRE on that machine uses the libraries as instructed in the .class file.
I hope I could make it clear to you.
In addition to the above answer, have a look at the libraries, which are found by:
right click on project
build path
configure path
libraries
You can check weather the JRE is added to your project or not
If it's not present click on add libraries and add it

Is there a way I can allow someone to run my Java program without them seeing the code?

So basically I made a terminal-dependent Java program and want to start handing it out, but I can't seem to find a way to do that without giving them my code. When making a .jar file it only works on Windows.
When trying to run on Linux (CentOS) I get
Exception in thread "main" java.lang.UnsupportedClassVersionError:
and
Could not find the main class: MyClass. Program will exit.
I'm using the command java -jar MyJar.jar to run them on both. I am using JavaMail in this program so perhaps that could be an issue. There is only one class and it has a main in it.
If anyone even has any alternatives to using a jar file to deliver the program where the user can't read the code that'd be great too. Thanks!
The JDK that you've actually used to compile and build your Jar is more recent than compared to the one installed on your client's Linux machine. That's when JVM throws an
Exception in thread "main" java.lang.UnsupportedClassVersionError:
Ask your client to report his Java version by running the following on console
java -version
Then if you're using Eclipse set the target runtime of your project accordingly. If you're compiling your Java source files on your own use
javac -target 1.4 source/MyClass.java
To me it sounds like the problem is that you compiled the code on an incompatible version that you're running the code on. For example, you compiled it with JDK 1.7 but the CentOS OS has only the java 1.5 version installed. Here is some more detailed information on the matter.
As for the title of your question, no, you don't have to give them the java source for them to run it. So something else is going wrong. Most likely the answer I gave.
Regarding question title:
Obfuscate your code. Dont print your name directly, use a generator method.
http://en.wikipedia.org/wiki/Obfuscation_(software)
http://www.yworks.com/en/products_yguard_about.htm
http://www.allatori.com/
http://www.zelix.com/
http://www.sable.mcgill.ca/JBCO/
In addition to the version issue others have described, and the fact that you don't need to distribute the .java files, you'll need to set up a Class-Path entry in the manifest file of your application jar file to reference the JavaMail jar file, and you'll have to deliver your application jar file and the JavaMail jar file to users. That can be tricky to get right. You might want to look at One-JAR to solve that problem.

Are both the JRE and the JDK required to run a JAR file?

It is possible to run a JAR file locally. The next step is to run it on a different PC.
The question is whether the JRE, the JDK or both are required to run the JAR file?
The JDK contains the JRE.
Most program only need the JRE (Java Runtime Environment) but some programs need the Compiler at runtime in which case you need the JDK.
If you have the JDK, you don't need the JRE as well.
To run a jar file you only need java.exe(windows). JDK is the development kit for Java and JRE is the runtime. JDK contains JRE.
In the comments on the accepted answer nobalG asked, "Why the compiler is needed if jre is already there?"
At the time of writing I did not have enough reputation to comment, so I replied here instead.
I had a situation where I wanted to write code that compiles other code at runtime and then uses that compiled code. In my case I was creating a tool that could take a test class based on a particular framework, compile it, load the class, and extract test data from it so that the data could be used as part of an end-to-end test. In order for this tool to run properly it must be run with the JDK so that it can use the Java compiler.
To run a jar file you only need the JRE. You can run the jar file with the following command:
java -jar [jar file Name]
You only need JRE.
If the jar file you are trying to run has the Main-Class: <classname> header present in manifest file, then you can simply run the jar file by the command:
java -jar [your jar file name]
If the manifest file does not have that entry (and you know the fully qualified class name of the class containing main function), then you can run the jar file by the command:
java -cp [absolute path to jar file] [full qualified class name containing the main function]
JRE is enough to run
JDK is used for development
You need a JRE but not the JDK. The JRE is the java runtime environment and java code cannot be executed without it. The .jar is a compiled java file can and this needs the java runtime environment to be run.
You want to run the jar file; so you just need Java Runtime environment ( i.e. JRE).

Categories

Resources