How to run Java program from any arbitrary directory inside Terminal - java

I saw some other similar posts, but they didn't seem to answer my question.
I have a package of java files, and the java functions inside one file call java functions from a different file. Right now I am limited to run my program from the parent directory of gitlet as shown in the screenshot. Is there a way to run my java program from any arbitrary directory?
P.S. I am hoping to get a solution that can be done just inside terminal.
screenshot of package

From anywhere
java -cp path/to/my/compiled/classes/directory1 fully.qualified.name.of.BootstrapClass

Is there a way to run my java program from any arbitrary directory?
Yes, you can add the base directory that contains the compiled .class files to the CLASSPATH environment variable.

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 make VS Code recognize .class file?

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

jar file with source code only

Suggest I have a HelloWorld.jar file, this file contains only HelloWorld.java , which is the source code of the application.
Will it be possible to run this jar file and execute the application, even though I don't have HelloWorld.class?
Yes, it can be done: See the javax.tools api. It is not easy, but it can be done....
You will likely be better off with a script that unjars the file, compiles it, and runs it.
Directly? No. java accepts only classfiles. To use source it must be compiled with javac. Nothing keeps you or a utility from compiling the source files to class files and using those, however.

build multiple shared libraries

Hi I have a java program which has to invoke a native program, and this native program are given by two so files. So I create my so file in order to use this native program APIs to do something for my java program. I was trying to merge two so files with my created so file into single one, and run my java program. However, it seems that it failed this way. To be more concrete, here is my example.
I have a java program A which has to invoke some native code. Therefore I've written some native code and built it as a shared library (called: C.so).
Unfortunately, the native code I've written have to use other code which is in other so files. (A.so, B.so)
Thus, any ideas how to compile my so file with A.so and B.so in order to make my java program work?
I'm assuming the following:
When you link c.so, you are listing a.so and b.so on the command line.
When you run ldd on c.so, you see a.so and b.so.
When you run, you set -Djava.library.path to include the directory containing all three.
When you run, you do NOT set LD_LIBRARY_PATH to include the directory containing all three.
You will get the desired results if you set the LD_LIBRARY_PATH environment variable to include the directory with the libraries in it.
For more explanation, and an alternative, see https://github.com/bimargulies/jni-origin-testbed.

Associate Java class-files to run on double-click on Windows

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 :)

Categories

Resources