Cannot read property MyToast.java of undefined - java

I have been following this Tutorial to achieve Write Java code in nativescript and use directly in typescript
But I got an error Cannot read property 'MyToast' of undefined
app.component.ts:
import {Component} from "#angular/core";
let application = require("application");
declare var org:any;
#Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public onTap() {
org.example.MyToast.showToast(application.android.context,"You pressed the button","short");
}
}
I have create MyToast.java class in platforms-> src -> java -> org ->example :
package org.example;
import android.widget.Toast;
import android.content.Context;
public class MyToast{
public static void showToast(Context context,String text ,String StrDuration ){
Toast.makeText(context,text, Toast.LENGTH_SHORT).show();
}
}

Did you perform a build? Metadata for JavaScript->Java invocation will not be generated in a simple tns run android where a build has not been triggered.
If you touch platforms/android, a build will not be provoked, as that directory is not being watched.
tns build android - this will ensure that, when making manual changes in platforms/android, a build will be triggered.
Offtopic: Also, I am happy to share that you will be able to create those classes directly in your App_Resources/Android directory in the 4.0 release of NativeScript, without worrying for your custom class being wiped clean when changing workstations, cleaning your project.

Related

IntelliJ does not recognize fillStateContainer, getDefaultState(), or getPlacementHorizontalFacing() Forge 1.16.5

I am making a custom two block long model called "littleguys:operating_table" and I watched tutorials to make it face the direction I want when it is placed. I made a custom OperatingTable class here:
package com.soliid.littleguys.blocks;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.StateContainer;
import net.minecraftforge.common.ToolType;
public class OperatingTable extends HorizontalBlock
{
public OperatingTable()
{
super(AbstractBlock.Properties.of(Material.STONE)
.harvestLevel(1)
.harvestTool(ToolType.PICKAXE)
.sound(SoundType.STONE)
.requiresCorrectToolForDrops()
.strength(3.5f, 4.0f)
);
}
#Override
protected void fillStateContainer (StateContainer.Builder<Block, BlockState> builder)
{
builder.add(FACING);
}
#Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite());
}
}
The #Override gives me an error reading
Method does not override method from its superclass, getDefaultState() gives me Cannot resolve method 'getDefaultState' in 'OperatingTable', and getPlacementHorizontalFacing() gives me Cannot resolve method 'getPlacementHorizontalFacing' in 'BlockItemUseContext'.
I now realized that these methods are not in OperatingTables's superclasses (HorizontalBlock and Block) but I want to know which methods are now used instead. There are no errors in the registry of RegistryObject<Block> OPERATING_TABLE or RegistryObject<Item> OPERATING_TABLE_ITEM.
This class is not complete but I cannot continue until I resolve the error.
There are several possibilities, but most cases are cache issue.
Please try invalidate cache and restart IntelliJ.
If issue persist, then sync Gradle project manually.
In build.gradle, on line 34, mappings is defined as channel: 'official', version: '1.16.5'. This should be changed to channel: 'snapshot', version: '[snapshot version]'. The version I used was '20210309-1.16.5'. Then, rebuild the gradle in Terminal using gradlew genIntellijRuns.

import libraries on eclipse 2021 fails Example: org.apache.commons.dbcp.BasicDataSource

I am migrating a Java code that is running on Java 8 and I need to compile it to run on Java 17, so Just downloaded eclipse 2021-09 import the code from Git, change project facet selecting Java, add to Java Build Path the libraries.
libraries added
This is the code:
package itksoluciones.avl.lst.dboperation;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class PoolConexionUC {
public static DataSource dataSourceUC;
public PoolConexionUC() {
inicializaDataSourceUC();
}
private void inicializaDataSourceUC() {
BasicDataSource basicDataSourceUC = new BasicDataSource();
basicDataSourceUC.setDriverClassName(DataConexion.driverUC);
basicDataSourceUC.setUsername(DataConexion.userUC);
basicDataSourceUC.setPassword(DataConexion.passUC);
basicDataSourceUC.setUrl(DataConexion.urlUC);
basicDataSourceUC.setMaxActive(Integer.parseInt(DataConexion.maxActiveUC));
basicDataSourceUC.setMaxIdle(DataConexion.defaultMaxIdleUC);
basicDataSourceUC.setMaxWait(DataConexion.defaultMaxWaitUC);
basicDataSourceUC.setRemoveAbandoned(DataConexion.removeAbandonedUC);
basicDataSourceUC.setRemoveAbandonedTimeout(DataConexion.removeAbandonedTimeoutUC);
basicDataSourceUC.setValidationQuery(DataConexion.validationQueryUC);
basicDataSourceUC.setTestOnBorrow(DataConexion.testOnBorrowUC);
dataSourceUC = basicDataSourceUC;
}
}
This are the two errors:
Import org.apache could not be resolved
and
BasicDatasourceCore could not be resolved to a type
Errors
As you could see from the error the library seems available, also it autocompletes when typing.
Also one of the solutions suggested by eclipse is to import org.apache.commons.dbcp.BasicDataSource. (after doing it by double clicking on the suggestion first error is resolved but the second one persists).
What is also very curious is that if I copy paste the class PoolConexionCore to PoolConexionCore2. The problem is gone in PoolConexionCore2.
BUT I HAVE MANY CLASSES WITH THE SAME PROBLEM so I really appreciate any suggestion to avoid having to be dedicated to copy pasted for several days.
After doing copy paste

How can I call methods from a different java project?

It's as the title says. I'm trying to call methods defined in another Java project. Is there a way I can do that? I tried import statements but that didn't work.
EDIT:
So here is what is sitting in the code now in terms of imports:
enter image description here
and here are some of the functions I want to call that are in the other project:
enter image description here
What I've tried is:
import com.example.cs320EthicsPlayer.api.*
but that doesn't work it just says import can't be resolved.
Where the 2 projects are located:
enter image description here
I'm not too familiar with mvn directories but we are using maven. The methods I want to call are from the cs320EthicsPlayer folder (project) and the file I'm calling it from is from partyinthebackend (another project). I haven't called on the other project at all, and that's what I'm trying to figure out.
Class Path for the file I'm trying to call the functions from:
enter image description here
Let's say we have a class Test inside a package com.example :
package com.example;
public class Test {
public static String getHalloWorld (){
return "Hello world";
}
}
All we need if we want to use our class Test in another package is to use import like that
import com.example.Test;
class OtherPackage {
public static void main(String[] args) {
String geeting = Test.getHalloWorld();
System.out.println(geeting);
}
}
You should remember anything you want to use in another package it should be public.
So just check where is the method, which package and class include the method you are trying to import.
Let's fix your problem now:
try
import com.example*
Now you import the whole package, but you should remember you can import and use just the public method from the package example.
Update:
I see you have updated your question again, and you want to use maven, I think that will answer your question :
Java project dependency on another project
I hope that answers your question.
If it is in the same project but different package you can just doing
import package name...
If it is a complete different project you can't import them. You need to re-insert the methods.

Run feature file with cucumber java

Im trying to run a simple feature file with java and intellij and I cant seem to get it working.
Ive set up my Cukes test runner class
package config;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(monochrome = true,
features = {"src/test/resources/features/"},
dryRun = false,
glue = "com.ecs.googleuat"
)
public class RunCukesTest {
}
feature:
Feature: home
Scenario: home
Given I am on the home page
step definitions:
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
public class MyStepdefs {
#Given("^I am on the home page$")
public void iAmOnTheHomePage() {
System.out.println("Hello");
}
}
project structure:
Im using a maven project with the java cucumber plugins.
When I run the feature file I get the following error:
Undefined step: Given I am on the home page
1 Scenarios (1 undefined)
1 Steps (1 undefined)
0m0.000s
You can implement missing steps with the snippets below:
#Given("^I am on the home page$")
public void i_am_on_the_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Process finished with exit code 0
Please help me understand what Im doing wrong.
Thanks
Not 100% sure but I think the issue is with the method name. The easy solution just copy-paste the suggested step definition inside MyStepdefs and remove exception and add your println and then run. Also try removing the glue com.ecs.googleuat you added.
Please follow this for further help!
Though I would strictly recommend going ahead with java-8.
Anyways, If you are comfortable with Java-8. Remove the glue inRunCukesTest.java. And update your MyStepdefs.java
public class MyStepdefs implements En {
public Stepdefs() {
Given("^I am on the home page$", () -> {
System.out.println("Hello");
});
}
}
En automatically implements default Glue for you.
Also, use appropriate dependencies. Follow this for same.
Edit:
For Java 8 please make sure that if you are using IntelliJ Idea, then Project SDK is enabled to 1.8.

Need help setting up intellij java project with multiple .java files from scratch

Edited to restart question from scratch due to complaints. I am a newbie to this format and to intellij so please excuse...
I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. My issue is I have two class files I am trying to integrate. NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work.
I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.
I have gotten it working by making a package under the src directory and adding both files to that package. This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line. I have been working all semester so far with everything in one source file so it hasn't been an issue until now. I don't remember using packages in the past under eclipse to do this.
Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?
The files in question reside in package named nettraffic in src directory.
NetTraffic.java
package nettraffic;
public class NetTraffic {
public static ProcessPacket pp;
public static void main (String args[]) {
pp = new ProcessPacket();
pp.PrintOut();
}
}
ProcessPacket.java
package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {
public ProcessPacket() {
}
public void PrintOut() {
System.out.println("Test");
}
}
Note there is no real functionality in these at this time. Just trying to get the class import syntax correct before continuing. Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.
public class NetTraffic {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal. Put it in a constructor or method.
public class NetTraffic {
public NetTraffic() {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
}

Categories