I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there
Related
I read and searched almost everywhere, yet i have no answer.
I have a project with multiple .java files, and i need to export them as a single .jar file.
One of the main questions is based on the fact that i don't have a main class.
What can i do to go through that ? Shall i create a main but never run it? Or there's any way around?
Offtopic ( might be usefull): i want to do this in order to use the jar file in IKVM
Thank you!
You do not need a main method to create a jar. There are many such jars out there such as utilities packages. They don't contain any code that would make sense to execute on it's own since it only contains code that makes your application easier to write.
First compile your code using Netbeans or command line javac utility. This will give you .class files.
Now for creating the jar... You can simply create the jar (which is basically a glorified zip archive) by using the command-line jar tool.
jar -cvf my archive.jar myprogram/
Make sure that the file path within the jar archive match the package name of the classes. It is not uncommon to accidentally get an extra layer of directories.
You probably should have searched stackoverflow first since how-to-create-a-jar-file-in-netbeans seems to cover exactly what you're looking for.
Do you want to use the jar as a external library or a program itself?
If it will be a external library, you don't need a main class. In eclipse you can export as a jar file (select classes-> right click-> export as jar).
If it will be a program itself, then you need a main class.
You really need to create main class file, write code in function main(String args[]) which use your java classes and compile it by javac.
I'm trying to use a .class file I downloaded for a uni project. I don't have the .java file to go with it. I am using netbeans.
I tried just adding it to the project src folder
I tried using "add JAR/folder" on libraries and adding the directory containing it
I also tried creating a JAR file of the directory containing it and adding that
Greatly appreciate any suggestions
You should do two things:
Create a jar of the class file you received
Create an overview which methods the classfile offers
For the latter you have at least two options. One is to use a decompiler (some authors of APIs deny you to use this) like JD-GUI. The second options is to use javap, which comes with your JDK (I link to Java 8, but it exists in prior versions too). Simply call javap yourfile.class and you will see which method signatures the class offers.
But the easiest way to see the classes / methods inside the .class file is JD-GUI, so if you are not running into any legal issues use that approach.
I have a .java and .class file which i put under a folder inside my pydev project in Eclipse (because im primarily using python).
Inside my python script i wanted to call the java class file using os.system.
os.system('java -mx1500m D:\\projects\\socialsense\\src\\ss\\samplefile\\test')
However it says that my class file is not found. What is wrong?
I expect that you can do that. However, most people would probably put the java source and python source in separate directory trees, and also not put .class and .java files in the same tree. (If you lump everything into the same place you'll cause yourself pain when you try to implement a "clean" rule in your build file ... or when you want to check your project into version control.)
Your immediate problem is that you've got the command syntax for the java command wrong. The name of the entry point class is specified by giving a fully qualified class name, not a pathname. And you probably need to use the -cp option as well.
For details on how to do this right, refer to the java manual page.
If there's one thing that annoys me about Java it's that you can't double-click a class file so as to run. I assuming there's an entry in the registry that has to be edited to do this but I haven't a clue.
So, as it says on the tin. Does anyone know how to associate Java class files to run on double-click on Windows (I aiming for Windows 7 here but I'm sure there'd be no difference in the three most latest releases)? It would make my life (and I'm sure many other people's) much easier!
Udpate: I've seen answers relating to making a JAR out of the class in question and running it that way. However useful, that is not exactly what I'm looking for here. I'm effectively looking for Windows itself to invoke java with the class on double-click, with the correct arguments.
if classpath doesnt matter too much, easily done with a simple batch file runjava.bat or so that is associated with .class files in the explorer (via right click >> open with..)
#echo off
REM change to folder where the class file resides
cd %~d1%~p1
REM execute the class by calling its name without file extension
start java %~n1
The double-clickable JAR solution is the most common plain Java distribution method. There'd be a number of issues with trying to execute .class files directly, with the classpath the one that pops first to mind.
That said, if you wanted to support the very simplest possibilities in your development environment, you could conceivably implement a script that
inspected the .class file for the full class name (including package and inner class name)
walked up the directory tree to the root of the file's class path
(optionally included any common lib directories in the classpath)
invoked Java for the determined class
Then you could register your shiny script as a handler for .class files. But since you're in the development environment, aren't you happier with your IDE doing that?
For a .class file to run, needs in first place to have "something" to do, that is, that .class should contain a main method. Not all the .class do have one.
One thing you can do, is to wrap your app ( a number of .class files ) inside a jar file.
For short, you just need in addition to your classes a manifest file that says, where the main method is:
jar -cmf yourmanifestfile.mf doubleClickApp.jar *.class
And that's it, the doubleClickApp.jar is now executable with a "doubleClick" gesture.
When you install the Java Runtime Environment, it registers .jar files as an association in Windows. If you double-click on a .jar file, it will open it using Java. For this to work, you need to make sure you have a manifest defined that points to the class to run. Your class file to be run must have a main method that will be called.
Let's assume you have a class named 'com.TheClass.class' on disk. If you want to have this able to run with double click, create a file in a new directory called META-INF/manifest.mf. Put this into it:
Manifest-Version: 1.2
Main-Class: com.TheClass
Zip (or use the jar command) both your class up with this manifest directory and file. Rename it to mine.jar. Double click on it and it should launch your class with the Java runtime.
http://justaddhotwater.webs.com/javaexec.htm
This software makes it possible to run your Java classes by double-clicking them.(Windows ONLY).
The easiest way that I have found was creating a shortcut on the same folder than the .class file. Then right click on it and go to properties. Change the field Target to java NameOfClass, finally double click the shortcut :)
I've got a very small set of classes built up in a custom package hierarchy with one console app class that employs them. Everything works fine from JCreator and from a command prompt.
I'd like to build a second console app that re-uses that same package.
As a Java newbie, what is the quickest, dirtiest method to do that?
My main concern was avoiding copying the package directories over to the new console app's directory.
Using JCreator, I didn't have any problems adding the package directory to the project and compiling and running. But when I tried to run the console app from the command line, it couldn't find the classes in the package hierarchy.
In Visual Studio, you just add a reference...
What you want to do for both apps is create a jar file with a Main-class definition in the var manifest. There's a good bit of information on this in the Java Tutorials, but the gist of it is just that you'll create a jar file with the jar tool, and then make a little wrapper to run it as
java -jar myfile.jar
If you do not wish to copy your class files from your first application, then you need to set up the classpath used when you run java from the command line to include the location of those files.
Make sure you also include the location of your newly created class files.
Using a tool like process exlporer, you should be able to see the command line which JCreator used to launch your application.
(Typically IDEs also include lots of command line parameters for being able to connect to your application and debug it, etc.)
If I read this correctly, you have a class I'll call A that references packages in package pkg and now you wish to create a new class B that also uses the classes in pkg.
One option is to create a pkg.jar file that contains all the classes in pkg and then create separate jar files to hold A and B. In the manifest files for A.jar and B.jar you can include a Class-Path element to include pkg.jar
Then so long as pkg.jar is sent along with A.jar or B.jar, they would each reference the pkg.jar without having to worry about specifying the classpath on the command line.
Some details here: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html