setting class path in Java - java

I used to run eclipse on my laptop. I have installed and used Secunia PSI application in order to update the apps, but once I done that I cannot run Eclipse.
This is what I have:
I set the PATH to the directory which contains javaw.exe app. The path at the PATH variable is the same as of CLASSPATH variable.
So, what are your resolutions? Done, it works now. I just simply needed to add '.;' at the end of the path link at the PATH.
Cheers

As the error message clearly states, Eclipse cannot find the entire JVM.
Add the directory that contains javaw.exe to your PATH.

Better to add JAVA_HOME as a new system variable in the same screen where you see the class path by clicking New and providing the folder location for JAVA root installation folder e.g. below:
Variable Name= JAVA_HOME
Variable Value= C:\Program Files\Java\jdk1.7.0_09

Related

Confusion over current working directory in File’s getAbsolutePath method

The file’s getAbsolutePath has the tendency that if a relative path or just the file name is provided in the file constructor, then it will resolve the absolute path by prefixing the current working directory.
The definition of current working directory is the directory where we run our Java Program.
In my example, my java program resides inside D:\my-app\src\App.java
When I do a System.out.println(System.getProperty("user.dir"));
Then it should ideally print D:\my-app\src
But it is actually printing D:\my-app
Why is it so?
When you start an application from IntelliJ, its working directory is set according to what's set in "Working directory" in the "Run/Debug Configurations" dialog.
There, you can set the working directory to a fixed value such as D:\my-app\src. Another option is to use a path variable. The list of variables that are available apparently can depend on many things - open the dialog and see what's available. One option that I see is $FileDir$ which I imagine would be the directory of the source code file.
I think I found the solution. Intellij was actually causing the issue by implicitly setting the src folder as the source folder. I found it by going to the module setting within Intellij.
When I ran the same from a command prompt, the correct working directory i.e. D:\my-app\src\ is selected.

Verify that ANDROID_HOME has been added to your path

I am trying to set up react-native (for Android) on my macOs but the app is failing to build so I am retrying it by following this link:
https://reactnative.dev/docs/environment-setup
In one of the steps, we have to add something to our $HOME/.bash_profile. I open it using vi .bash_profile and then paste the commands. Next, I am asked to 'Verify that ANDROID_HOME has been added to your path by running echo $PATH.'
When I run echo $PATH., I get this:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users//Library/Android/sdk/emulator:/Users//Library/Android/sdk/tools:/Users//Library/Android/sdk/tools/bin:/Users//Library/Android/sdk/platform-tools
I don't see the word ANDROID_HOME anywhere. Is there a problem here? Maybe with the path? Or is it being correctly verified?
I am running this in the root directory.
EDIT:
I managed to successfully setup everything and run the default AwesomeProject as well (from the root directory). However, when I try to run another project, it does not work. I see this:
SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at '/Users/mycompany/insta/app/android/local.properties
What's the problem? Which steps from the setup should I repeat inside this particular folder? Should I make another bash profile? I don't get what path to add in the local.properties
You can find all the environment variable documentation here: https://developer.android.com/studio/command-line/variables
According to the dev website:
If ANDROID_HOME is defined and contains a valid SDK installation, its value is used instead of the value in ANDROID_SDK_ROOT.
If ANDROID_HOME is not defined, the value in ANDROID_SDK_ROOT is used.
If ANDROID_HOME is defined but does not exist or does not contain a valid SDK
installation, the value in ANDROID_SDK_ROOT is used instead.
Try echo $ANDROID_HOME. If that returns nothing try echo $ANDROID_SDK_ROOT. One of them should return your SDK directory. If not export one of them in your bash_profile as a env variable.
Easiest solution is to do a quick install of Android Studio and selecting the option to install SDK tools. This easily sets up your environment for android. All you need to do is add the SDK tools directory to your path once the install completes.
AVD images are pedantic if you change any directories and throw SDK_ROOT errors if you change things. Most problem free solution is to either download and run the installer or download the SDK tools, run the install and specify the path directory.
Your PATH variable is being correctly modified.
Echo your ANDROID_HOME variable to see what it is. It should be /Users//Library/Android/sdk/. When you use this variable in PATH, it's automatically being expanded when you echo it. You won't see "ANDROID_HOME" in the PATH.
create local.properties file inside android folder and paste sdk.dir={ANDROID_SDK_PATH}, then restart react-native server.
This problem has to do with two things:
local.properties file
ANDROID_HOME (SDK path)
Firstly,
For the local.properties, first make sure that the file is there in your project folder/android. If the file is there open it and make sure that the sdk.dir is pointing to your android SDK directory.
and if the file is not there create it and paste this:
sdk.dir = /Users/USERNAME/Library/Android/sdk
Replace USERNAME with your username
Secondly,
make sure that the ANDROID_HOME is there in the system variables and is also pointing to your android SDK directory.
or create new system variable named ANDROID_HOME and set the value your android SDK directory path if it's not there

How to Download Java and run a file on command line

I know this is SUPER basic. I am very new to all of this. I tried to download java and run a helloworld by following these instructions: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
I got an error when I tried to use the "javac" command to compile. This is what appears in the command prompt:
C:\Users\USer18\Desktop>javac HelloWorldApp.java
'javac' is not recognized as an internal or external command, operable program or batch file.
Does this mean I downloaded java incorrectly? When I downloaded it, there were 3 different things to choose from, but I could only choose one, so I chose the first one. I tried to download java again and select the second one, but it said it didn't work.
Thanks in advance for helping me!
From the tutorial you linked, it tells you to "consult the installation instructions" found here: https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html
For Windows JDK, the instructions linked are here: https://docs.oracle.com/javase/8/docs/technotes/guides/install/windows_jdk_install.html#CHDEBCCJ
The part you need to look for is "Updating the PATH Environment Variable"
Updating the PATH Environment Variable
If you do not set the PATH variable, you need to specify the full path
to the executable file every time you run it, such as:
C:> "C:\Program Files\Java\jdk1.8.0\bin\javac" MyClass.java
It is useful to set the PATH variable permanently so it will persist
after rebooting.
To set the PATH variable permanently, add the full path of the
jdk1.8.0\bin directory to the PATH variable. Typically, this full path
looks something like C:\Program Files\Java\jdk1.8.0\bin. Set the PATH
variable as follows on Microsoft Windows:
Click Start, then Control Panel, then System.
Click Advanced, then Environment Variables.
Add the location of the bin folder of the JDK installation to the PATH variable in System Variables. The following is a typical value
for the PATH variable:
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.0\bin
Note:
The PATH environment variable is a series of directories separated by semicolons (;) and is not case-sensitive. Microsoft Windows looks
for programs in the PATH directories in order, from left to right.
You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored.
If you are not sure where to add the JDK path, append it.
The new path takes effect in each new command window you open after setting the PATH variable.
When ever we execute any command, it is searched in the directory where we are current in or mentioned in PATH environment variable. The oly thing which you need is just add <path of yourjdk>\bin to PATH
Windows 10 and Windows 8
In Search, search for and then select: System (Control Panel)
Click the Advanced system settings link.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows 7
From the desktop, right click the Computer icon.
Choose Properties from the context menu.
Click the Advanced system settings link.
Click the Advanced system settings link.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Reopen Command prompt window, and run your java code.

Adding external jar in classpath placed in home dir

I am working on a java project which consumes an external jar from another project. This external jar cannot be checked-in in lib folder as it evolves continuously. So we have planned to keep the latest jar inside a folder,say 'ExtJar', under User's Home dir.
The question is how do I modify classpath to point to this jar from home dir?
So, I was looking for something like and should work for all OS platforms-
classpathentry kind="lib" path="MyHomeDir/ExtJar/myExternalJar.jar"
where MyHomeDir is a variable I defined as per the link -
- Use Eclipse classpath variable to replace absolute "sourcepath" path?
I looked into above link to add a variable but apart from this I also want this variable to point to appropriate home dir depending on OS, i.e C:\Users\\ExtJar\ for Windows OR /home//ExtJar for linux etc.
Is there any way to programatically modify value of this variable to point to home dir after evaluating which OS its being run on?
The point of a Classpath Variable is that it's value is not the same for all workspaces; each workspace defines where the variable points to.
You can still use a Classpath Variable to solve your problem, though. For example, create a variable called EXT_JAR_HOME and point it to your C:\Users\your.name\ExtJar folder. Then in the project build path, use Add Variable... to select and **Extend...* it, selecting the actual JAR file. That will result in the project's build path having an entry like EXT_JAR_HOME/ExternalJar.jar. Then each developer workspace will just need to, one time, defineEXT_JAR_HOME` and point it to the correct path. Linux users' actual location will look different than Windows users, obviously.
The point is, Classpath Variables must be defined in each workspace, that's how they're designed to work.

Java VM Options: Set a -Djava.library.path relative to project folder

I would like to set the -Djava.library.path VM option to a specific folder. However, it is not relative to my project folder so that I could say for example:
-Djava.library.path=native\windows
(The folder native is in the project folder.)
Do you know if this is somehow possible to set it like above and not with -Djava.library.path=C:\...?
You actually can set a relative path. For example if you start your program a specific folder, you can access libraries in a folder "libs" right next to it by setting the path to "../libs" e.g.
In my own project with native libraries I have this in my shell script:
-Djava.library.path=../../native/unix
Hope this answers your question.
I'm not sure if you are asking how to refer to a relative directory on windows, or how to set this path without the -Djava.library.path=... parameter. So, I will answer both.
To set a relative path, use:
-Djava.library.path=.\windows
To set this path on Windows without using -D, augment the PATH environment variable:
setenv PATH %PATH%;C:\path\to\folder
On Linux/Mac, set/augment the LD_LIBRARY_PATH with this folder location.

Categories