How do I map the Run Configuration from IntelliJ to VSCode? - java

I want to have the same run configuration I have on Intellij the same as I do for VS Code. But I am not sure how to go about doing that. I tried the launch.json on VSCode but I am not sure what fields I have to put to make it work.
Anyone with experience with this?
Here is what I am referencing:
Essentially when I click the Run Button on the top right of the intellij it runs that particular run configuration picture.
Is there a way for me to do the same on VSCode?

If .vscode/launch.json doesn't exist in your workspace, you could click the link "create a launch.json file" and Java Debugger will create a initial launch.json file with some base configuration for your Java application.
Type a "" in launch.json, VS Code will provide a list of completion suggestions on the supported configs. If you want to add environment variables for your program, you can type "env": { "key": "value"}.

There is this project which lets you convert Jetbrains run configurations to VSCode
jetbrains-vscode

Related

Eclipse plugin dev: how to add default VM arguments by code?

While developing some plugins based on Eclipse, I need to add some default VM arguments when user right click-> "run as" -> "java application" or "spring boot app", is there any open api I can call or any extension point?
Just like the snapshot below, the argument "-Daaa=bbb" is added by default.
Any help is appreciated.
You need to write your own plugin and need to replicate/extend Java launch configuration
(of type Java application) from JDT and add -vm argument programmatically.
But there is one more easy way: Try LcDsl.
LcDsl provides a way of defining Eclipse launch configurations in a textual way. The file extension used is '.lc' It provides some obvious and some non-obvious advantages over the Eclipse launch configuration solution.
For more info look here and check demo videos. Also there was a discussion going on to include this plugin in JDT itself, see this bug entry.
A typical Java launch configuration looks like this:
java configuration LcJavaMain {
project com.wamas.test;
main-class com.wamas.test.JavaMain;
memory min=64m max=256m;
vm-argument '-Dmy.arg=value';
argument 'cmdArg';
}

Regarding execution of .robot files for robot framework in IntelliJ

As it is specified everywhere for executing robot scripts you have to create maven project.I created the maven project and have added all the dependencies and plugin required for the execution. But when i create the maven project then by default a class ic created as AppTest.java in my src/test folder.Now when i add this .robot file in my test folder or main folder.It is not getting executed.Moreover i have a confusion how to execute these scripts.Please help me with this.Where should i place these file so that they could be executed.
I believe that at this time (7 months after your question) you've found a solution for the problem. If yes, I suggest you share it with the community.
Here's my solution, assuming that you're using Google Chrome:
Open the Edit Configurations window for your maven project, select the tab "Parameters", and in the field "Command Line" type "robotframework:run". It should look like this:
Then you select the tab "Runner" and add the property "webdriver.chrome.driver=/path-to-the-webdriver". It shoud look like this:
(note: uncheck the "Skip tests" check box. Mine is checked on the screenshot because I was testing other things.)
What's a little frustrating is that we can be misled by the error messages which say that you have to set the environment variable "webdriver.chrome.driver". In this case, we're tempted to add this variable to the field "Environment variables".
However, IntelliJ creates its own environment on run. That's why the field "Environment variables" doesn't work. When you add that property to the "Properties" box, the IntelliJ will convert it into a parameter in the run command, which will create the proper environment for the project.
Something like this:
/usr/lib/jvm/java-8-oracle/bin/java
-Dmaven.multiModuleProjectDirectory=/home/acampos/study/robotframework01
-Dmaven.home=/home/acampos/programs/idea-IU-172.4343.14/plugins/maven/lib/maven3
-Dclassworlds.conf=/home/acampos/programs/idea-IU-172.4343.14/plugins/maven/lib/maven3/bin/m2.conf
-javaagent:/home/acampos/programs/idea-IU-172.4343.14/lib/idea_rt.jar=36889:/home/acampos/programs/idea-IU-172.4343.14/bin
-Dfile.encoding=UTF-8
-classpath /home/acampos/programs/idea-IU-172.4343.14/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher
-Didea.version=2017.2.5
HERE!!!
-Dwebdriver.chrome.driver=/home/acampos/programs/chromedriver/chromedriver robotframework:run
So, when you run your maven project it will run the goal that's described on the Parameters tab, Command line field.
I hope it helps. And, again, if you've found another solution, please share with us.
Good luck!

IntelliJ IDEA Java How to make Run configurations automatically select Main Class

My friend and I both have IntelliJ and just want to run plain old java. Every time I make a new java class, I can automatically run it. The configurations just work.
When my friend wants to run it, he has to go to edit configurations and type in the class name. We went through the exact same setup. How do we make it automatically the main function in the class?
Friend's
MINE Macbook Pro
When you're in a java file that contains a public static final void main(String[] arg) method you can press Ctrl+Shift+F10 to make and run a temporary run configuration for the current class. This can save a bit of time when setting up the configurations.
Alternatively you can try to share the .idea directory with him, as it contains all of the project's settings. I'm not sure what other options will be shared though.
it's easy, Your friend must do this and this setting is changed in the "Default Project Structure..." dialog. Navigate to "File" -> "Other Settings" -> "Default Project Structure...".
Next, modify the "Project language level" setting to your desired language level.
IntelliJ IDEA 12 had this setting in "Template Project Structure..." instead of "Default Project Structure..."
Creating files in the src(source folder) where the jvm expects to find the java classes instead of creating them in the project root folder helped solve the problem for me.

Visual Studio Code - Java Classpath is incomplete. Only syntax errors will be reported

I am making my first steps with java, after some extensive experiences with python. The script I am running is a simple Java Swing Gui, that compiles and runs fine from the command line and within VS Code.
To set up the java debug environment, I used the lauch.json settings suggested on the github site https://github.com/k--kato/vscode-javadebug.
Unfortunately, every time I open the folder that contains the script, I get the following error message:
Warn: Classpath is incomplete. Only syntax errors will be reported.
I have no idea if the problem comes from within VS Code, of if it's some other configuration issue, such as the java set up....
My working platform is Linux Ubuntu, Gnome Shell.
Can anybody help?
This is the script:
//file name = SimpleEx.java
import java.awt.EventQueue;
import javax.swing.JFrame;
public class SimpleEx extends JFrame {
public SimpleEx() {
initUI();
}
private void initUI() {
setTitle("Simple example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SimpleEx ex = new SimpleEx();
ex.setVisible(true);
});
}
}
and this is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Java",
"type": "java",
"request": "launch",
"stopOnEntry": true,
"cwd": "${fileDirname}",
"startupClass": "${fileBasename}",
"options": [
"-classpath",
"${fileDirname}"
]
},
{
"name": "Java Console App",
"type": "java",
"request": "launch",
"stopOnEntry": true,
"cwd": "${fileDirname}",
"startupClass": "${fileBasename}",
"options": [
"-classpath",
"${fileDirname}"
],
"externalConsole": true
}
]
}
I know this is an old question, but anyone who stumbles here and needs a quick and easy fix may find it here. Install the extension: Eclipse New Java Project.
It emulates the behavior of the Eclipse action create Java Project and should produce the results you need.
Just press Ctrl + Shift + P and type New Java Project (it will pop up after a few letters) and follow the simple directions. (it just asks for the name of the project).
It will create the project and files needed and you won't have to worry about that classpath error.
Then just create your class files as normal in the src folder of your new project and proceed with your programming!
In VS Code just right-click on the src (Your Project Source Folder) and select the " Add folder to Java source path" option. If it didn't' worked, then try to remove it first by selecting "remove the folder from Java source path" (this will not result in any data loss) and then adding it again.
Restart once when done.
Hope it works for you.
Since the Microsoft Visual Studio Code ecosystem is rapidly evolving for Java, there is a handy solution that enormously helps to generate - in just a few steps - a functioning Java project structure to use with VS Code.
There are, of course other solutions to work with Java, like NetBeans, but I have always liked VS Code a lot and just waited until something more easy came up to get back to using it.
The quite easy solution I found is using MAVEN. Maven creates the whole project structure and initial configuration files in your home folder and you can immediately open it with VS Code and run it without any hassle.
Of course, you will have to have the Java extensions installed, as explained here.
The solution I found here on the net was a little outdated, so I have made some adaptations.
This is the process on a Linux machine:
Check if you have MAVEN installed
Type into a terminal:
mvn --version
If maven is not installed, the output will suggest the installation command;
Invoke the maven quickstart archetype to generate your new project;
Inside the terminal type or copy:
mvn archetype:generate
This will open a scary list of over 2000 achetypes to choose from. The good news is that if you don't fill in a number, maven-archetype-quickstart will be automatically selected, so just press ENTER.
Choose a version from the list and type in the number: I picked the suggested number = 6
Choose a value for property groupId:
com.mycompany.app
Define a value for property 'artifactId' (this will create the folder in your home directory) :
my-app
Define value for 'version' : 1.0
Define value for property 'package' :
com.mycompany.app
Confirm the options and the project will be generated.
Start VS Code with the new project
In the terminal type :
code ./my-app
Configure a Launch.json file
In the top left of the debugger, on the right side of the green, where it says "no configuration", choose "Java" from the drop-down field and the .json file will be auto-generated.
Configure a Task.json
Go to Task in the menu bar;
Configure tasks ......
Create task.json from template ....
Select "maven" from the drop-down list and the .json file will be auto-generated.
You are good to go.
This warning is displayed when you open a java file that the redhat.java extension can not determine a classpath for. To get the full benefits of the extension use either a project with maven pom.xml (soon also gradle), or least default eclipse setting files aka .classpath .project files.
I got this error along with Cannot resolve the modulepaths/classpaths automatically, please specify the value in the launch.json during compilation.
All I had to do was open Command Palette in View or with Ctrl+Shift+P and then run the command Clean Java Server Language Workspace and everything started working normally again.
searching for this i found that vscode right now only recognizes maven projects, or eclipse projects so it needs a .classpath file. So the best option is create the project with maven first and then open with vscode.
i know it's too late but you can simply solve this if you convert your project to eclipse with this simple command mvn eclipse:eclipse (assuming you are using maven) at your root directory this will add .classpath file. in this case your src/* files will be project files
Tested on vscode and coc-java in vim too
In case this helps anyone else, I have to change this line in .classpath
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
to
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main">
I just had to change the path and it worked
Just press ctrl+shift+p and type Create Java Project and then click no build tools
and it will ask to Select folder, then choose the folder and it ask for creating the file name, give the name to file It will create the project and files needed and you won't have to worry about that classpath error. Then just create your class files as normal in the src folder of your new project and proceed with your programming!
I stumbled upon this issue and resolved it on my own. I was trying to compile and run my java file from outside of the folder it was inside. That's when I got the same "classpath" error op mentioned above. Once I moved into the directory in which the files were located, I was able to run without issue.
create .vscode/settings.json with the following content:
{
"java.project.sourcePaths": [
"<project_path>/src"
],
"java.project.referencedLibraries": [
"<libs_path>/**/*.jar"
]
}
For the users facing this issue using visual studio code, you can try uninstalling the java extension you have been using and reinstall it.

Defaults for Eclipse run configurations

I'm writing a Java library with a lot of jni code. Pretty much every test case needs to load my jni dll, and I have a lot of test cases. In order to run the test cases out of Eclipse's Junit launcher, I have to create a run/debug configuration and edit the VM arguments and environment variables.
I would like a way to set the VM arguments and environment variables to a default for the entire project and have new run configurations include the default entries. From what I can tell, Execution Environments maybe do something like this but I seem to need the PDE to get them to work(?)
Specifically, I want to enable assertions on my project by default and include the path to my native dll in the PATH environment variable. I can't use the "Default VM Arguments" setting in the JRE definition panel because my dll depends on a number of others and java.library.path isn't used for dependency resolution, PATH is. Is there a way to make Eclipse do what I want?
So, here's what I did.
First, my specific problem was that I have a lot of run configurations, I create new ones on the fly, and I needed certain system properties set for unit tests. Setting them under the 'args' tab of run configurations was undesirable for my workflow. Also, I wanted the same command-line args set for all of my tests. I also don't run my app from inside eclipse. It's a dev-environment only.
So my solution was to add it to the command-line of my JRE. Preferences -> Java -> Installed JREs. Clicking edit gives you a window where you can specify default VM args. I just set the system properties I need for testing there.
Hope this helps.
How long does it take to run all of your tests for the project?
If the answer is Not long then create a project-wide JUnit launcher. If occasionally you would need to do a run on a single test case ( in order to debug or something ), you can copy all your settings from the project's junit launcher. I think you can even clone your project launcher to run a specific test case.
Run->Run Configurations...
Create new JUnit launcher.
On 'Test' tab select Run all tests
in selected {...}
Connfigure JVM options, classpath,
environment etc. for this launcher
Optional, but highly recommended. On
Common tab -> Save as -> Shared
file, and check-in launcher with
your project
One more thing I would do is to define a system property in launcher VM arguments, check for this property in #Before function and throw exception if the property is not set. This way you will know that your test fails because it is not using the right launcher.
If I understand your question correctly, I think Alexander is on to the idea with cloning the project launcher. Eclipse lets you duplicate launch configurations with a single click - simply setup one configuration with the parameters you require and click the button in the top left to duplicate it whenever you create a new one.

Categories