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?
Related
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
I'm currently having trouble with compiling a project I'm working on.
I use some libraries, namely lwjgl, slick and jorbis. I've added them to my classpath and I am compiling with the following command
javac -g -classpath ".:./lib/lwjgl.jar:./lib/slick.jar:./lib/jorbis-0.0.17-2.jar" -d ./bin #files
The files file is simply a list of every class I'd like to compile.
The compiler then throws a lot of errors like:
foo\bar.java:25 cannot find symbol
bar\foo.java:3 package does not exist
What's especially strange is that compilation worked on a previous project where I only used one library instead of multiple.
I use Sublime Text 3 and bash on Linux Mint to code / compile my work (if that helps.)
All help is very welcome =]
[EDIT]:
I got the project to compile now.
I also tried to compile this on Windows (using git bash) and I get the same errors, but not on Linux anymore. I'll try to compile using a batch script on Windows, but at least I can compile it =]
Also works when I pack the finished project into a .jar.
Thanks y'all.
///WARNING- I DO NOT KNOW ANY LANGUAGES/HAVE NO USED ECLIPSE BEFORE\\
I'm currently trying to get a .jar file compiled in Java8 to compile in Java7, but seem to be having trouble, I have the source code intact so that is not the problem. It compiles fine, but the finished .jar is smaller than the original by a large margin. I believe the problem lies in my inexperience with Eclipse meaning I problably did something wrong.
Source Folder Structure
src\com\darkblade12\enchantplus...java files
Contents Of Each Folder
If you have some doubts about eclipse being behind the problem, try to compile it directly using the Command line Interface with the target version version of java by using javac.
Example for Windows:
set JAVA_HOME=C:\Program Files (x86)\Java\jre1.7.0_21
javac com/darkblade12/enchantplus/*.java
jar cvf program.jar -C com/darkblade12/enchantplus
Then compare your jar generated by eclipse and this generated directly from the command line.
Another way to verify is to open the jar file with and archive manager like 7-zip and verify its content.
I have been researching this for weeks and can't seem to get it figured out.
I have a Java program that I have written using NetBeans. It has several imports or .jar files it relies on. It runs fine in NetBeans. But I can't figure out how to call the .jar files and compile from the bash command line. I am using a Mac. I have read several posts on this and none so far have made sense to me. There are 26 imports being used in the program. I don't know if I need to use Ant or specify -CP or Classpath to compile. Surely I don't have to type each one of the .jar files out to compile this from the bash command line?
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
You should be able to simply use ant with the build.xml in the Netbeans project directory. It should "just work" for you. The project will likely not build WITHOUT Netbeans installed (if you tried moving the project to a different machine, for example), but with Netbeans, it should work out of the box.
If you don't have Ant installed, you'll need to install it.
Addenda:
To build it, if you have Ant installed, you should be able to simply go the project directory, where the build.xml file is, and type "ant", and it should build and put stuff in the dist directory.
If you go in to the dist directory and type java -jar yourapp.jar, it should run, because the manifest in the jar will point to the nearby lib jar files. If you want to distribute the app, there are different options for java, such as making a Mac compatible application, or a Windows EXE, you'll need to search for those. Or you can simply distribute the contents of the dist directory and write a script to do that whole java -jar yourapp.jar command.
If you are using Java 1.6 or above, you can toss all 26 jar files into a folder and simply add
-cp folder_name/*
as your classpath argument.
Some other options are 1.) type all 26 jar files on the command line (using the -cp argument as you have mentioned) 2.) use Ant or Maven or some other build tool and list those 26 jar files in the config file for said build tool or 3.) write a quick-and-dirty shell script that will set the CLASSPATH environment variable for you and then run your javac command.
I'm trying to compile a java program that is using JavaDB/Derby. On the command line I can run
java org.apache.derby.tools.sysinfo
without errors, but the following line in several of the files causes the error in my question title:
import org.apache.derby.client.am.SqlException;
causes
package org.apache.derby.client.am does not exist
I've done a fresh installation of JavaDB, but I don't think that matters. I've compiled this project once before, and I KNOW I didn't have JavaDB installed. I just had a directory at the top level of the project folder called lib with all of derby's .jar files inside. And I'm pretty sure I didn't have to set any environment variables either.
How can I fix this error? If I need to provide any more information, I will be happy to do so.
I'm using Windows 7 and jdk1.7
Sounds like you have an issue with the JavaDB JARs not being on your classpath. Make sure you specify them using -cp or -classpath on your javac command.